-2

I have a string "platform:17.01.02" and I want to search for "17.01.02" which I'm giving through a variable.

a = "platform:17.01.02"
b = "17.01.02" (has to taken from user)

a.search(/b/)

The above statement search for "b" and not for variable value "b". Can any one help how can i search that?

4
  • b doesn't look like a regex, so you should not use search Commented Mar 21, 2017 at 11:28
  • a.search(b) is enough here Commented Mar 21, 2017 at 11:29
  • You are looking for a.indexOf(b) Commented Mar 21, 2017 at 11:40
  • a.search(b) worked for me. Commented Mar 21, 2017 at 11:44

1 Answer 1

-1

Try this:

var a = "platform:17.01.02";
var b = "17.01.02";

console.log(a.search(new RegExp(b))); //9

If you just want to know if a contains b, use:

a.indexOf(b) >= 0
Sign up to request clarification or add additional context in comments.

1 Comment

that will yield unexpected results when the user accidentally types in regex metacharacters

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.