I have a problem with regular expression:
var regex = new RegExp('(^|\\s)' + clsName + '(\\s|$)');
What does (^|\\s)
mean? Isn't it equal to (^|\s)
, what does (^|)
mean?
Am I right, it means that the string should start with any letter or white space? I tried to test with browser and console.log but still can't get any solution.
In all tutorials \s
is used to be a space pattern not \\s
.
Ok i got it, the problem was:
When using the RegExp constructor: for each backslash in your regular expression, you have to type \\
in the RegExp constructor. (In JavaScript strings, \\
represents a single backslash!) For example, the following regular expressions match all leading and trailing whitespaces (\s); note that \\s
is passed as part of the first argument of the RegExp constructor:
re = /^\s+|\s+$/g
re = new RegExp('^\\s+|\\s+$','g')