The example in this article describes how JS uses regular expressions to implement the keyword replacement and bold function. Share it with everyone for your reference, the details are as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>正则替换</title> </head> <body> <script type="text/javascript"> var str = "使用正则表达式把关键字替换加粗,文字加粗"; var word = "加粗"; str = str.replace(new RegExp("(" + word + ")","ig"), "<strong>" + word + "</strong>"); document.write(str); </script> </body> </html>
The screenshot of the running effect is as follows:
I hope this article will be helpful to everyone JavaScript Programming helps.
For more JS using regular expressions to implement keyword replacement and bold function examples, please pay attention to the PHP Chinese website!