Case insensitive search and variable - get position in JavaScript

Is it possible to get the position in string using a case insensitive JavaScript search() function and variable ?
0
give a positive ratinggive a negative rating
Hi,

To use a case insensitive search() function in Javascript with variable, you have to use a RegExp object. In RegExp, you can make the pattern and set case insensitive search:

<script>

function addLink(content, variable){

var p = new RegExp("(?!\\s)" + variable, 'i');
var v = content.replace(p, "<a href='https://www.example.com' target='_blank' >"+ content.substr(content.search(p), variable.length) +"</a>");
return v;

}

var c = addLink('Microsoft Office Home & Business inlcudes Outlook', 'outlook');
alert(c);

</script>

Share on FacebookShare on TwitterShare on LinkedInSend email
1 answer
x
x
2024 AnswerTabsTermsContact us