This article describes how JS uses regular expressions to implement keyword replacement bold function. Share it for your reference, 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>regular replacement</title></head><body><script type="text/javascript">var str = "Use regular expressions to replace keywords with bold, text bold";var word = "bold";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:
PS: Here are two very convenient regular expression tools for your reference:
JavaScript regular expression online testing tool:
http://tools.VeVB.COM/regex/javascript
Regular expression online generation tool:
http://tools.VeVB.COM/regex/create_reg
For more information about JavaScript related content, please check out the topics of this site: "JavaScript Regular Expression Skills", "Summary of JavaScript Replacement Operation Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Data Structure and Algorithm Skills", "Summary of JavaScript traversal algorithms and techniques", "Summary of json operation techniques in JavaScript", "Summary of JavaScript Errors and Debugging Skills" and "Summary of JavaScript Mathematical Operation Usage"
I hope this article will be helpful to everyone's JavaScript programming.