0

This is my string converted into javascript object.

{"text" : "Must consist of alphabetical characters and spaces only", regexp:"/^[a-z\\s]+$/i"}

I need regexp to use it for validation but it won’t work because of the double quotes and \s escape sequence.

To make it work the value of regexp must be {"text" : "Must consist of alphabetical characters and spaces only", regexp : /^[a-z\s]+$/i}.

I also used this new RegExp(object.regexp) and any other way I can possibly think but with no luck at all.

Any help is appreciated!

cweiske
  • 30,033
  • 14
  • 133
  • 194
  • So if I understand correctly, the problem is not translating the regex, but encoding it properly. So what are you trying to do? Is this a piece of Json, or are you trying to write and execute a .js file? Why is your question tagged 'jquery'? – GolezTrol Feb 18 '15 at 15:29
  • 1
    `new Regex(object.regexp)` should work: http://jsfiddle.net/tcsmcjst/1/ – Rory McCrossan Feb 18 '15 at 15:30
  • @RoryMcCrossan But it doesn't work... It's trying to match against `//^[a-z\\s]+$/i/` rather than just the `^[a-z\\s]+$` part with case insensitivity. – Joseph Marikle Feb 18 '15 at 15:35
  • Then you need to change the regex string, it's only doing what you tell it to. – Rory McCrossan Feb 18 '15 at 15:37

3 Answers3

3

Try split-ing out the part that you want, before putting it into the new RegExp constructor:

var regexVariable = new RegExp(object.regexp.split("/")[1]);

That will trim off the string representation of the regex "boundaries", as well as the "i" flag, and leave you with just the "guts" of the regex.

Pushing the result of that to the console results in the following regex: /^[a-z\s]+$/

Edit:

Not sure if you want to "read" the case insensitivity from the value in the object or not, but, if you do, you can expand the use of the split a little more to get any flags included automatically:

var aRegexParts = object.regexp.split("/");
var regexVariable = new RegExp(aRegexParts[1], aRegexParts[2]);

Logging that in the console results in the first regex that I posted, but with the addition of the "i" flag: /^[a-z\s]+$/i

talemyn
  • 7,822
  • 4
  • 31
  • 52
  • Your code is working for `/^[a-z\s]+$/i` however when i tried regular expression for email address from formValidation which he got it from this [link](http://stackoverflow.com/questions/46155/validate-email-address-in-javascript) and it won’t work. Is it possible to make it flexible that it will adopt to any valid regular expression? Thanks for the help. – Russell Pabon Feb 18 '15 at 18:01
  • Can you share how it is coming in the object? I'd need to be able to see exactly what the string is in your code, to know what would need to be changed. – talemyn Feb 18 '15 at 18:09
  • 1
    I got it working now. My fault was to use `urldecode` in PHP instead of keeping it as encoded string. Now using your code I only add `decodeURIComponent(object.regexp).split("/")` and everything worked accordingly. – Russell Pabon Feb 18 '15 at 18:50
1

Borrowing the example @RoryMcCrossan made, you can use a regular expression to parse your regular expression.

var object = {
    "text": "Must consist of alphabetical characters and spaces only",
    "regexp": "/^[a-z\\s]+$/i"
}

// parse out the main regex and any additional flags.
var extracted_regex = object.regexp.match(/\/(.*?)\/([ig]+)?/);
var re = new RegExp(extracted_regex[1], extracted_regex[2]);

// don't use document.write in production! this is just so that it's
// easier to see the values in stackoverflow's editor.
document.write('<b>regular expression:</b> ' + re + '<br>');
document.write('<b>string:</b> ' + object.text + '<br>');
document.write('<b>evaluation:</b> ' + re.test(object.text));
Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
0

not used regex in Java but the regular expression itself should look something like :

"^([aA-zZ] | \s)*$"

If Java uses regular expression as I am used to them [a-z] will only capture lowercase characters

Hope this helps even if it's just a little (would add this as a comment instead of answer but need 50 rep)

Johan
  • 262
  • 1
  • 15