1

i am getting the following regex from a json from server

regexval = ^[A-Za-z\\'\\s\\.\\-\\,]{1,50}$

there is a problem with this regular expression it has a ' which is breaking my js code to test this regular expression. how do i make this a valid regex?

im doing something like this to test it

if(!regexval.replace(/\\/g,"\\").test(inputVal)) {
}

EDIT

following is the JSON

[
    {
        "ExtensionData": {},
        "Data1": null,
        "DisplayName": "First Name",
        "IsRequired": true,
        "LengthValidation": 0,
        "Name": "ServiceFirstName",
        "RegexValidation": "^[A-Za-z\\'\\s\\.\\-\\,]{1,50}$",
        "Type": "String",
        "ValidationMessage": "The field 'First Name' doesn't meet the format requirements"
    },
 {
        "ExtensionData": {},
        "Data1": null,
        "DisplayName": "Last Name",
        "IsRequired": true,
        "LengthValidation": 0,
        "Name": "ServiceLastName",
        "RegexValidation": "^[A-Za-z\\'\\s\\.\\-\\,]{1,50}$",
        "Type": "String",
        "ValidationMessage": "The field 'Last Name' doesn't meet the format requirements"
    }
]
5
  • doing \\ in there will just cause your regex to look for literal backslashes (multiple times) and literal ', s, ., -, and , Commented Aug 5, 2011 at 16:38
  • so how do i make the regex valid? because !regexval.test(inputVal) is not working Commented Aug 5, 2011 at 16:40
  • is this object that json is passing in a string format to begin with? Commented Aug 5, 2011 at 16:44
  • var regexval = /^[A-Za-z\'\s.\-,]{1,50}$/;. Commented Aug 5, 2011 at 16:44
  • i have posted the Json please take a look Commented Aug 5, 2011 at 16:51

3 Answers 3

3

The double backslashes are evaluated to literal backslashes in RegEx notation... or escaped backslashes in a string (which then escapes the next character when evaluated as a regex).

The RegEx build solution is:

regexval=/^[A-Za-z\'\s\.\-\,]{1,50}$/

The string-build solution is:

regexval = new RegExp("^[A-Za-z\\'\\s\\.\\-\\,]{1,50}$");

Both of these produce a JavaScript RegEx object which you can then call .test etc on.

if (regexval.test(inputVal)) {
  //... do something
}

Your Update

Ok so per your update, if you parse that above structure into a local JS object called jsonobj (for example... how to parse etc I assume you know) and you want to just test the first entry in the JSON array returned... you'd need to do the following:

//jsonobj - JS object after parsing your example JSON above
var regex=new RegEx(jsonobj[0].RegexValidation);
if (regex.test(inputVal)) {
    //... do your work here
}
Sign up to request clarification or add additional context in comments.

1 Comment

but still ^[A-Za-z\'\s\.\-\,]{1,50}$ is breaking i guess because of the ' what should i do? it says regexval.test is not a function
1

Can you change the single quote to a unicode character /u0027 or use the ASCII \x27 and have it work properly?

See it in action here: http://jsfiddle.net/TezdR/1/

SO, really you DON'T need to do the replace but can use the "string" as it is - here is an updated version where you can see I do the replace on FirstName but not on last - and they both should work the same: http://jsfiddle.net/TezdR/2/

1 Comment

how do i change it all i have is the json from the webservice
1
regexval = /^[A-Za-z\\'\\s\\.\\-\\,]{1,50}$/

you're missing the /s needed to make it a regex value

and it looks like you actually need

regexval = /^[A-Za-z\'\s\.\-\,]{1,50}$/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.