Home > Web Front-end > JS Tutorial > JS uses regular expressions to implement keyword replacement and bold function examples

JS uses regular expressions to implement keyword replacement and bold function examples

高洛峰
Release: 2017-01-14 09:35:36
Original
1819 people have browsed it

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>
Copy after login

The screenshot of the running effect is as follows:

JS uses regular expressions to implement keyword replacement and bold function examples

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!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template