3

I have to use regular expression which is set in javascript's object property, like below

var myObj = { mask : /([a-z]|[A-Z]|[0-9]|-|\.|\'|[&])/}

in some other function which will get myObj as parameter and in that function I need to test that if string contains any character not matching above regex. Example

function check(myObj , value){
//Want to code here to read myObj.mask and return false if value contains 
//any character not matching myObj.mask. as per above example if value contain any special character 
// like &#$% then i want to return false

}
1
  • Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. Commented Mar 2, 2014 at 9:43

2 Answers 2

2

Your regex is very redundant, you are doing lots of unnecessary escaping. You can use the shorter version:

/^([a-zA-Z0-9-.'&])*$/

Notice I added some anchors, they are necessary for RegExp.test();

var myObj = { mask : /^([a-zA-Z0-9-.'&])*$/ }

function check(myObj , value){
    return myObj.mask.test(value);
}

console.log(check(myObj, "abc1-.'&")); //true
console.log(check(myObj, "&#$%")); //false
console.log(check(myObj, "abc#")); //false

Explanation for /^([a-zA-Z0-9-.'&])*$/

  • ^ the beginning of the string
  • ( group and capture start group 1
  • [a-zA-Z0-9-.'&] any character of: a to z, A to Z, 0 to 9, -, ., ' or &
  • ) end of group 1
  • * group 1 zero or more times
  • $ end of the string

It is simpler than the original because [a-z]|[A-Z] is the same as [a-zA-Z]. Also, inside character classes (that is, between [ and ]), you don't have to escape ., ' or &.

If you can't change the expression

In that case, we can create a new RegExp object, adapting the one passed as parameter.

var myObj = { mask : /([a-z]|[A-Z]|[0-9]|-|\.|\'|[&])/ }

function check(myObj , value){
    return new RegExp('^'+myObj.mask.source+'*$').test(value);
}

console.log(check(myObj, "abc1-.'&")); // true
console.log(check(myObj, "&#$%")); // false
console.log(check(myObj, "abc#")); // false
Sign up to request clarification or add additional context in comments.

10 Comments

Would like to know a short explanation of your regex /^([a-zA-Z0-9-.'&])*$/
one doubt, what if ? is used instead * will that not be okay?
@user1671639 I added some explanation. Can you check if it good enough?
return new RegExp('^(' + myObj.mask.source + ')*$').test(value);
@rps With ? it will match nothing, so I guess it is not ok. Check this test: jsfiddle.net/acdcjunior/QFuxt/1 (open console F12). The () is necessary when you want to choose who the * or ? will affect. They also form groups, accessible through $1. No need to be sorry. Feel free to ask. If I can help, I gladly will.
|
0

This can be done by

return myObj.mask.test(value)

3 Comments

I think it's wrong answer(either @user1671639's too). OP wants to test that "if string contains any character not matching above regex". So 'abcd#' should be false since it contains '#'.
Thanks for response, but if value contains even one special character like $%^#, then will it return false?
yes Sagar it will. I am assuming u want abc# to return false. That's exactly what it will do. abc will return true. I am only telling u how to test a regex, not reformatting it

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.