2
var str = "Visit tEsT!"; 
var ara = "test";
let n = str.search(/\b + ara + \b/i); // Sadly code doesn't run
document.getElementById("demo").innerHTML = n;

I want find ara variable in str variable with regex search().

2
  • 1
    Pleas explain clearly what is the result you want.. Commented Jan 29, 2020 at 15:41
  • İ want find "ara" variable in "str" variable with regex search(). Commented Jan 29, 2020 at 15:43

5 Answers 5

3

You need to define a new regex from a string in order to use variables.You can do this with new RegExp(). You will also need the i option for case insensitive mode.

var str = "Visit tEsT!"; 
var ara = "test";
var regex = new RegExp(`\\b${ara}\\b`, 'i'); // Evaluates to "/\btest\b/i"
let n = str.search(regex);
document.getElementById("demo").innerHTML = n;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for fastly help :)
ara.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '') @FZs
2

You have to put variables like this in Regex:

var str = "Visit tEsT!"; 
var ara = "test";
let n = str.search(new RegExp(`\\b${ara}`,"i")); 
console.log(n);

More secure way is to remove special chars:

str.replace(/[^A-Za-z0-9\s\n]/g,"")

var str = "Visit tEsT!"; 
var searchstr = "test &#";
let n = str.search(new RegExp(`\\b${searchstr.replace(/[^A-Za-z\\s\\n]/g,"")}`,"i")); 
console.log(n);

1 Comment

Thank you for fastly help :)
1

In your regular expression, you cannot use the + operator as a string concatenator. Instead, you could use for example template strings and create the regular expression with new RegExp:

var str = "Visit tEsT!"; 
var ara = "test";
var regex = new RegExp(`\\b${ara}\\b`, 'gi');  // use 'gi' flags to search globally and ignore case
let n = str.search(regex);
console.log(n);

4 Comments

You can use + for concatenation. You also need to escape \b
Thank you for fastly help :)
@ButchMonkey Thanks for your advice. I thought I had to double escape but the PlayCode.io syntax highlighting confused me...
NP. I should clarify that you can use + for concatenation in new RegExp as it creates a regex from string. But you are correct that you cannot use + in /.*/g syntax as it will be evaluated
1

If you want the matched string, use match() instead. The return value n will be an array and its first value (n[0]) will be the matched string.

var str = "Visit tEsT!"; 
var ara = "test";
var regexp = new RegExp("\\b"+ara+"\\b", 'gi')
n = str.match(regexp);
document.getElementById("demo").innerHTML = n[0];

4 Comments

Thank you for your help too i maked search engine for my blog but if i use indexOf() visitor search value "a" then it show all articles ... İ just want to filter with regex to make results better...
Why do you want to show the index of the matched expression? That's what String.prototype.search() does.
let str = 'B ad ga'; let result = str.search('a'); document.write(result); but when i run this code, answer is: "2" so search() like indexOf()
Yes. It's exactly like indexOf except it expects a regular expression.
1

Using search and match multiple ways.

var str = "Visit tEsT!"; 
var ara = "test";
let n = str.search(new RegExp('\\b' + ara + '\\b', 'i'))

let n2 = str.match(new RegExp('\\b' + ara + '\\b', 'i')).index;

//document.getElementById("demo").innerHTML = n;

console.log(n, n2)

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.