通过正则表达式查找一个模式的所有实例
生活随笔
收集整理的這篇文章主要介紹了
通过正则表达式查找一个模式的所有实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這個功能就是一般的文本查找功能,比較實用,記錄下來,說不定以后可以用到
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head><meta charset="utf-8" /><title>string的match用法</title><style type="text/css">#searchSubmit{background-color: orange;width: 200px;text-align: center;padding: 10px;}.found{background: red;}</style> </head> <body><form id="textsearch"> <textarea id="incoming" cols="100" rows="10"></textarea><p>Search pattern: <input id="pattern" type="text" /></p></form><p id="searchSubmit">Search for pattern</p><div id="searchResult"></div> </body> <script type="text/javascript">window.onload = function(){document.getElementById('searchSubmit').onclick = doSearch;}function doSearch(){//獲取模式var pattern = document.getElementById('pattern').value;var re = new RegExp(pattern, 'g');//獲取字符串var searchString = document.getElementById('incoming').value;//替換var resultString = searchString.replace(re, "<span class='found'>$&</span>");//插入到頁面 document.getElementById('searchResult').innerHTML = resultString;} </script> </html>
?
轉載于:https://www.cnblogs.com/MockingBirdHome/archive/2013/04/29/3050483.html
總結
以上是生活随笔為你收集整理的通过正则表达式查找一个模式的所有实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 河南省第二届ACM程序设计大赛解题报告(
- 下一篇: 系统集成性研究