博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js的正则处理
阅读量:7049 次
发布时间:2019-06-28

本文共 1690 字,大约阅读时间需要 5 分钟。

1.search()方法,返回首次出现位置,如果找不到匹配则返回-1
 
InBlock.gif<!DOCTYPE html PUBLIC 
"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
InBlock.gif<html> 
InBlock.gif    <head> 
InBlock.gif    <meta http-equiv="content-type" content="text/html; charset=UTF-8"
InBlock.gif    <title>正则表达式</title> 
InBlock.gif    <script text="text/javascript"
InBlock.gif        alert("hello <zhangsan>.".search(/<\S+>/)); 
InBlock.gif    </script> 
InBlock.gif    </head> 
InBlock.gif    <body> 
InBlock.gif 
InBlock.gif    </body> 
InBlock.gif</html>
 
 
2.replace()方法
 
<
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
> 

<
html
> 

    
<
head
> 

    
<
meta 
http-equiv
="content-type" 
content
="text/html; charset=UTF-8"
> 

    
<
title
>正则表达式
</title> 

    
<
script 
text
="text/javascript"
> 

        alert("hello 
<
zhangsan
>.".replace(/
<
\S+
>/,"
<
张三
>")); 

    
</script> 

    
</head> 

    
<
body
> 


    
</body> 

</html>
 
替换匹配规则的字串
 
<
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
> 

<
html
> 

    
<
head
> 

    
<
meta 
http-equiv
="content-type" 
content
="text/html; charset=UTF-8"
> 

    
<
title
>正则表达式-去除尖括号
</title> 

    
<
script 
text
="text/javascript"
> 

        alert("hello 
<
zhangsan
>.".replace(/
<
(\S+)
>/,"$1")); 

    
</script> 

    
</head> 

    
<
body
> 


    
</body> 

</html>
 
 
3.match()
 
<
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
> 

<
html
> 

    
<
head
> 

    
<
meta 
http-equiv
="content-type" 
content
="text/html; charset=UTF-8"
> 

    
<
title
>正则表达式-分割邮箱
</title> 

    
<
script 
text
="text/javascript"
> 

      var arr="[email]xcf-007@163.com[/email]".match(/(\S+)@((\w+\.)+\w+)/); 

     document.write(arr);//xcf-007@163.com,xcf-007,163.com,163.    

    
</script> 

    
</head> 

    
<
body
> 


    
</body> 

</html>
   本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/105007,如需转载请自行联系原作者
你可能感兴趣的文章
05-spark streaming & kafka
查看>>
python杂记
查看>>
cd 简化命令
查看>>
LeetCode--205--同构字符串
查看>>
python-ConfigParser模块【读写配置文件】
查看>>
wireshark使用方法总结
查看>>
Window Server 2008 R2 TFS2010 安装前的准备
查看>>
20141123
查看>>
translucent 属性
查看>>
android listView嵌套gridview的使用心得
查看>>
[ES7] Descorator: evaluated & call order
查看>>
安卓动态调试七种武器之离别钩 – Hooking(上)
查看>>
从P6 EPPM 8 R3 到P6 EPPM 16 R1 有哪些改变?
查看>>
Android Studio2.0 教程从入门到精通Windows版 - 安装篇
查看>>
Linux 系统磁盘满处理方法
查看>>
Java HashMap Demo
查看>>
yaml官方介绍
查看>>
three.js模型
查看>>
网络流24题 餐巾计划问题
查看>>
基于 Android NDK 的学习之旅-----序言
查看>>