Skip to content
Advertisement

Bolding a keyword if found in a string

JavaScript

Greetings, Actually I want to bold a keyword if it is found in a string. I am using php preg_match() method for this purpose. My keywords are stored in an array and by iterating through each keyword I match it with my string and then bold it. I am facing a problem here. Both of the above strings are giving me different outputs. str1 is giving me stack overflow while str2 is giving me stack overflow. But for both cases both of the words should be bold. Please give me the solution to solve this problem.

I have this issue on https://www.paraphraser.site/

Advertisement

Answer

See below Example code for Reference, you can do as per given examples:

JavaScript

w*? matches any word characters before the keyword (as least as possible) and w* any word characters after the keyword.

And I recommend you to use preg_quote to escape the keyword:

JavaScript

For Unicode support, use the u flag and p{L} instead of w:

JavaScript

For your code you can do it Like below:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement