朱纯树博客
VPS测评推荐网站
cloudacead cloudacead

JavaScript 中使用正则表达式

可以用正则的方法:

search() 找到匹配的第一个字符串,返回位置,文档

match() 提取匹配的字符串,返回子串,文档

replace() 替换全部匹配的字符串,文档

RegExp.test() 检测字符串是否和正则匹配,文档

两种正则定义方法

var re = new RegExp('a', 'i');

var re = /a/i;

常用规则

/*
* \d 数字
* \D 除了数字
* \w 英文数字下划线
* \W 除了英文数字下划线
* \s 空白字符
* \S 除了空白字符
* . 任意
* 
* {n} 出现 n 次:如 \d{2} 表示 2 个数字
* {n,m} 出现 n~m 次:如 \d{2,4} 表示 2~4 个数字
* {n,} 最少出现 n 次
* + 若干个:如 \d+ 表示若干个数字
* ? 出现 0 或者 1 次:如 \d? 表示没有或者有一个数字
* * 出现 0 或者 多次,如 \d* 表示没有或者有一个/多个数字
* | 或者,如:1|2 表示 1 或者 2
* [] 或者,如 [abc] 表示 a 或 b 或 c
*           [a-z0-9] 表示从 a 到 z 或者从 0 到 9
*           [^a-z] 表示除了 a 到 z
* 
* i 忽略大小写
* g 全局匹配
* 
* ^ 行首
* $ 行尾
* 
* () 小括号内优先
* 
* */

一个在线验证正则表达式的网站

https://www.regexpal.com/

正则表达式30分钟入门教程

https://www.jb51.net/tools/zhengze.html

赞(2) 打赏
未经允许不得转载:VPS测评推荐网站 - 朱纯树博客 » JavaScript 中使用正则表达式

评论 7

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #0

    You made some decent points there. I looked on the internet for the difficulty and located most people will associate with with your website.

    zorivare worilon2个月前 (06-29)回复
  2. #0

    I like this web site so much, bookmarked. “American soldiers must be turned into lambs and eating them is tolerated.” by Muammar Qaddafi.

    hire a hacker online2个月前 (06-22)回复
  3. #0

    Your place is valueble for me. Thanks!…

  4. #0

    Wonderful web site. A lot of helpful information here. I am sending it to a few pals ans also sharing in delicious. And certainly, thank you for your effort!

    gralion torile3个月前 (05-13)回复
  5. #0

    Thank you for sharing excellent informations. Your site is so cool. I’m impressed by the details that you?¦ve on this blog. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for more articles. You, my pal, ROCK! I found simply the info I already searched all over the place and simply couldn’t come across. What a great web-site.

    Beatris Dizadare4个月前 (05-02)回复
  6. #0

    I like your writing style really loving this web site.

    Kevin Deppner4个月前 (04-25)回复
  7. #0

    I’ve been absent for a while, but now I remember why I used to love this blog. Thank you, I will try and check back more often. How frequently you update your site?

    zoritoler imol4个月前 (04-16)回复