Tuesday, December 25, 2018

Make regular expression patterns without wrapping double quotes in JavaScript!

function IsRegexMatchForName(subject) {
   let regExPattern = /^([A-Za-z\.'-]+[\s]*)+$/;
   let isMatch = !!subject.match(regExPattern);
   return isMatch;
}

 
 

...is more or less akin to...

function IsRegexMatchForName(subject) {
   let regExPattern = "^([A-Za-z\\.'-]+[\\s]*)+$";
   let isMatch = !!subject.match(regExPattern);
   return isMatch;
}

 
 

Note that there are some tiny differences between the RegEx of JavaScript and that of C#. This touches on them some. Note that the quoteless trick in JavaScript is kinda like the at symbol trick in C#! Some C#:

string foo = @"^([A-Za-z\.'-]+[\s]*)+$";

 
 

In both cases we are getting out of having to lead a backslash with a backslash.

No comments:

Post a Comment