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
12 Dec 2022 at 06:46 PM
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>

0
give a positive ratinggive a negative rating
13 Dec 2022 at 10:05 AM
Tim
Share on FacebookShare on TwitterShare on LinkedInSend email
x
x
2024 AnswerTabsTermsContact us