-1

I want my RegEx to work both on server and client side (with javascript) I'm decorating my ViewModels like this:

    [Display(Name = ConstantKeys.ValidationMessage_Gender_Name)]
    [Required(ErrorMessage = ConstantKeys.ValidationMessage_Gender_Required)]
    [RegularExpression(RegExKeys.Gender, ErrorMessage = ConstantKeys.ValidationMessage_Gender_RegEx)]

But I don't know if it's possible to use RegexOptions.IgnoreCase for the RegularExpression attribute...

How can I write a regular expression that matches these:

word1|wORD2|wOrD3 ...

(I want the regular expression to still be human readable) Thanks in advance.

CLARIFICATION: I want to match ANY words (not just word1-3) better example:

Man1|woMAN2|MoNkEY3
Zombo
  • 1
  • 62
  • 391
  • 407
Yovav
  • 2,557
  • 2
  • 32
  • 53
  • 2
    "I want the regular expression to still be human readable" -- Human readable by a human that can read regular expressions... – elclanrs Aug 13 '15 at 03:01
  • I meant human readable by humans that want to use regular expressions but don't want to dedicate their life to understanding the ins and outs of regular expressions :-) – Yovav Aug 13 '15 at 03:04
  • Check [(word\d\|?)+](https://regex101.com/r/hE1lI2/1) – Tushar Aug 13 '15 at 03:10
  • Are you primarily using js? If so why not just add all words into an array. Then iterate them into a new RegExp constructor function with the i flag?? – EasyBB Aug 13 '15 at 03:52
  • Also if your using a library of sorts you need to specify this. It looks like your using either ASP.net or c# with RegexOptions.IgnoreCase – EasyBB Aug 13 '15 at 04:02

2 Answers2

0

how about ([wW][oO][rR][dD])|([aA][nN][dD])|([Ss][oO])|([Oo][Nn])|([fF][oO][rR][eE]+[vV][eE]+[rR])

all chars inside [] means any of these chars so u can put the different

choices for the letters in

() groups a alternative a word togeter

| lets th reg choose any of the () words

word and so on forever all match the expression as well as

WORD AND SO ON FOREVER and anything inbetween.

Philipp
  • 137
  • 1
  • 2
  • 11
  • What about the 1, 2 and 3? – RobG Aug 13 '15 at 03:07
  • No, I want to have all the words listed - so if I have 50 words it's not going to look like a big mess... besides, if I take your approach - how can I add 3 words (do I use pipe sign as separator?) – Yovav Aug 13 '15 at 03:07
  • [0-9] means any 1 digit number, [0-9]+ multidigit – Philipp Aug 13 '15 at 03:09
  • My first example is not a good one... I want to be able to match different words (better example: "Man1|woMAN2|MoNkEY3") – Yovav Aug 13 '15 at 03:17
  • I may end up writing a function that construct this on the fly based on a list of words... but hope to find a cleaner solution... – Yovav Aug 13 '15 at 03:19
  • i dont fully see why ignorecase is not an option, because my solution is exactly the same – Philipp Aug 13 '15 at 03:30
0

If you are looking for the first word in a string, then go this way:

'asdasd ujh32d %^#@ mAN1'.match(/[\w]+/)

-> ["asdasd"]

If you want want to list all the words then:

'asdasd ujh32d %^#@ mAN1'.split(/[\W]+/)

-> ["asdasd", "ujh32d", "mAN1"]
Joshua Coussard
  • 204
  • 1
  • 7
  • Is that a JavaScript - .split(/[\W]+/) how can I write a RegEx to match each of the words inside of the string? – Yovav Aug 13 '15 at 04:00
  • This is [JavaScript] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split). Perhaps you should update your question with a specific entry string and expected output. It would be easier to help you. – Joshua Coussard Aug 13 '15 at 04:02