I have this code:
const ids = '[2][13]';
const myid = '1';
const result = ids.search('[' myid ']') != -1 ? 'found' : 'not found';
console.log(result);
this is returning found
whereas it should be returning not found
because id #1 is not present. However since [13]
has a 1
in it then it misinterprets it. How can I fix this so it searches for the EXACT value match?
CodePudding user response:
For the String.prototype.search()
you are using a RegExp
which treats some characters as reserved for the pattern matching - angle brackets for your case.
ids.search(/\[1\]/);
If you are assembling the expression manually, the backslash is also treated differently because it's parsed twice. The first parsing is for a conversion of string into a RegExp
object (which uses \
for special characters such as \n
, \t
, ...) and the second parsing is when the regular expression is utilized.
If you escape it only once (\[
) it'll create a regular expression of [
which is a reserved character, therefore you need to use \\[
so that the first parsing converts it to \[
and then a regular expression \[
i.e. the literal value of [
is used for pattern matching.
var myid = 1;
'[2][13]'.search(new RegExp(`\\[${myid}\\]`));
// or as pointed out in the comments:
'[2][13]'.search(`\\[${myid}\\]`);
CodePudding user response:
search
converts any string you give it to a regular expression. []
is special in regular expressions, it defines a character class.
I suspect you want includes
, not search
, which looks for exactly what you pass it.
const result = ids.includes("[" myid "]") !== -1 ? "found" : "not found";
(Or with a template literal:
const result = ids.includes(`[${myid}]`) !== -1 ? "found" : "not found";
)
CodePudding user response:
what your are trying to do can be achieved with .indexOf
this returns index of exact match occurance or -1 in not found
you can check details about indexOf
here indexOf
var ids = '[2][13]';
var myid = '1';
var r = ids.indexOf('[' myid ']') != -1 ? 'found' : 'not found';
console.log(r)
CodePudding user response:
var ids = '[2][13]';
let isMatch = function(n)
{
return ids.match(/\[(\d )\]/g).includes(`[${n}]`);
}
console.log(isMatch(1), isMatch(2), isMatch(3), isMatch(10), isMatch(13));
false true false false true
CodePudding user response:
The nasty trap is that you need to double-escape the square bracket
Whatever you pass into the .search()
method is assumed to be a regular expression. In a regular expression, the "[" and "]" are the delimiters for a set of characters, allowing a match with any one of those characters.
For example the regular expression [A-Z34]
will match with any capital letter or the digits 3 or 4.
Unfortunately your data contains [
and ]
as characters within it.
You need one \
for Javascript and one \
for the regular expression parser, so that your code correctly looks for exactly the string [1]
.
const ids = '[2][13]';
const myid = '1';
const result = ids.search('[' myid ']') !== -1 ? 'found' : 'not found';
console.log(result) // found
const result2 = ids.search('\[' myid '\]') !== -1 ? 'found' : 'not found';
console.log(result2) // found
const result3 = ids.search('\\[' myid '\\]') !== -1 ? 'found' : 'not found';
console.log(result3) // not found