Home > Web Front-end > JS Tutorial > js regular search match() and replace() usage examples

js regular search match() and replace() usage examples

高洛峰
Release: 2017-01-22 14:04:01
Original
1363 people have browsed it

The example of this article describes the usage of regular search match() and replacement replace() in js. Share it with everyone for your reference. The details are as follows:

<html>
<head>
<script type="text/javascript">
//string.match(正则):正则查找字符串,返回符合正则的字符或字符串
function t1(){
 var con = document.getElementsByName(&#39;content&#39;)[0].value;//需要查找的内容
 var reg = /\Bhi\B/g;//匹配中间有hi的单词。g为模式增强符,表示全局匹配
 alert(con.match(reg));
}
//string.replace(正则,用什么替换):返回被替换后的string
function t2(){
 var con = document.getElementsByName(&#39;content&#39;)[0].value;//需要查找的内容
 var reg = /<script.*<\/script>/;//把javascript代码替换为空
 alert(con.replace(reg,&#39;&#39;));
}
</script>
</head>
<body>
<textarea rows="5" cols="30" name="content"></textarea><br />
<button onclick="t1();">正则查找字符串match()</button><br />
<button onclick="t2();">正则查找字符串replace()</button><br />
</body>
</html>
Copy after login

I hope this article will be helpful to everyone’s regular expression learning.

For more js regular search match() and replace() usage examples related articles, 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