19

I am getting regex string from json object (yes its dynamic and will be always be string) i want to test this with textbox value.

But even if i pass valid input text it does not pass regex condition

code :

var pattern = "/^[A-Za-z\s]+$/";
var str = "Some Name";
pattern = new RegExp(pattern);
if(pattern.test(str))
{
    alert('valid');
}
else
{
    alert('invalid');
}

Fiddle :- http://jsfiddle.net/wn9scv3m/

3
  • It's expecting a space at the end of each word. if Name had a trailing space it would pass Commented Aug 27, 2014 at 8:45
  • 2
    @Derek It does not need a space at the end.[] mean any character from the box.Doesnt matter the sequence. Commented Aug 27, 2014 at 8:51
  • You're right, I was seeing the [] as () for some reason. See my answer for a working regex solution. Commented Aug 27, 2014 at 8:56

5 Answers 5

25

Two problems:

  • You need to escape the backslash.
  • You need to remove the forward slashes on the beginning and end of string.

Corrected code:

var pattern = "^[A-Za-z\\s]+$";
var str = "Some Name";
pattern = new RegExp(pattern);
if(pattern.test(str))
{
    alert('valid');
}
else
{
    alert('invalid');
}

http://jsfiddle.net/wn9scv3m/3/

Sign up to request clarification or add additional context in comments.

1 Comment

For completion, I would also add that flags can be passed as a second argument to new RegExp. So for e.g. /[a-z]+/i you could pass new RegExp("[a-z]+", "i").
10

Use regex-parser:

const parseRegex = require("regex-parser")

parseRegex("/^hi$/g")
// => /^hi$/g

Comments

6

This should work for you (jsfiddle: http://jsfiddle.net/wn9scv3m/9/):

var pattern = /^[(\w)|(\s)]+$/; // using / regex constructor...
var altPattern = "^[(\w)|(\s)]+$"; // using quotes and new RegEx() syntax...
var regex = new RegExp(altPattern);
var str = "Some Name";
if (str.match(pattern) != null && regex.test(str) != null) { // check using both methods
    alert('valid');
}
else {
    alert('invalid');
}

Comments

5

As far as I can see in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions you are combining two methods to declare RegExp. If you are using the string variant, then don't include the "/" character before and after the expression, example:

var pattern = "^[A-Za-z\s]+$";
pattern = new RegExp(pattern);

If you like the /regexp/ form better, then use it without quotes:

pattern = /^[A-Za-z\s]+$/;

Comments

-1

this should work

var str1 = "SomeName"; //true
var str2 = "SomeName123"; //false

function MyRegex(val) { 
    var pattern = /^[A-Za-z\s]+$/;
    var match = pattern.exec(val);
    return match !== null && match[0] === val;
} 

alert(MyRegex(str1));
alert(MyRegex(str2));

1 Comment

The pattern input is var pattern = "/^[A-Za-z\s]+$/"; So there is no exec() function for strings.

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.