19

I'm trying to detect an occurrence of a string within string. But the code below always returns "null". Obviously something went wrong, but since I'm a newbie, I can't spot it. I'm expecting that the code returns "true" instead of "null"

var searchStr = 'width'; 
var strRegExPattern = '/'+searchStr+'\b/'; 
"32:width: 900px;".match(new RegExp(strRegExPattern,'g'));

5 Answers 5

38

Please don't put '/' when you pass string in RegExp option

Following would be fine

var strRegExPattern = '\\b'+searchStr+'\\b'; 
"32:width: 900px;".match(new RegExp(strRegExPattern,'g'));
Sign up to request clarification or add additional context in comments.

2 Comments

could you try with searchStr only without \\b part? your string supposed to work without that.
@Fares, you need '\\b' with 2 '\'s, not '\b'.
6

You're mixing up the two ways of creating regexes in JavaScript. If you use a regex literal, / is the regex delimiter, the g modifier immediately follows the closing delimiter, and \b is the escape sequence for a word boundary:

var regex = /width\b/g;

If you create it in the form of a string literal for the RegExp constructor, you leave off the regex delimiters, you pass modifiers in the form of a second string argument, and you have to double the backslashes in regex escape sequences:

var regex = new RegExp('width\\b', 'g');

The way you're doing it, the \b is being converted to a backspace character before it reaches the regex compiler; you have to escape the backslash to get it past JavaScript's string-literal escape-sequence processing. Or use a regex literal.

Comments

3

The right tool for this job is not regex, but String.indexOf:

var str = '32:width: 900px;',
    search = 'width',
    isInString = !(str.indexOf(search) == -1); 

// isInString will be a boolean. true in this case

Documentation: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/String/indexOf

1 Comment

not it is wrong because the following sentence is also true: var str = '32:width: 900px;', search = 'wid', isInString = !(str.indexOf(search) == -1); console.log(isInString);
1

Notice that '\\b' is a single slash in a string followed by the letter 'b', '\b' is the escape code \b, which doesn't exist, and collapses to 'b'.

Also consider escaping metacharacters in the string if you intend them to only match their literal values.

var string = 'width';
var quotemeta_string = string.replace(/[^$\[\]+*?.(){}\\|]/g, '\\$1'); // escape meta chars
var pattern = quotemeta_string + '\\b';
var re = new RegExp(pattern);
var bool_match = re.test(input); // just test whether matches
var list_matches = input.match(re); // get captured results

Comments

0

You can use back tick symbol to make your string dynamic "`".

var colName = 'Col1';
var result = strTest.match(`xxxxxxx${colName}`);

by injecting ${colName} in to the text, it can be editable dynamically.

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.