javascript正则表达式——元字符
元字符(Metacharacter)是拥有特殊含义的字符:
元字符 描述
(1) . 查找单个字符,除了换行和行结束符。
例子:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>点</title>
- </head>
- <body>
- <script type="text/javascript">
- str='index and php 2 and Php and pHp 3 and indox and 20 indax and indBx andpcp and pp and p p and p-p and p_p and p\np and p9p';
- arr = str.match(/p.p/ig)//查找单个字符,除了换行和行结束符。
- alert(arr);
- </script>
- </body>
- </html>
效果如图:
(2)\w 查找单词字符。(查找字母、数字、下划线,注意不包括“-”中间横线,如下例子)
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>反斜线小写w查找单词字符</title>
- </head>
- <body>
- <script type="text/javascript">
- str='index and php 2 and Php and pHp 3 and indox and 20 indax and indBx andpcp and pp and p p and p-p and p_p and p\np and p9p';
- arr = str.match(/p\wp/ig)//反斜线小写w查找单词字符
- alert(arr);
- </script>
- </body>
- </html>
效果图:
(3) \W 查找非单词字符。
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>反斜线大写W查找非单词字符</title>
- </head>
- <body>
- <script type="text/javascript">
- str='index and php 2 and Php and pHp 3 and indox and 20 indax and indBx andpcp and pp and p p and p-p and p_p and p\np and p9p';
- arr = str.match(/p\Wp/ig)//反斜线大写W查找非单词字符
- alert(arr);
- </script>
- </body>
- </html>
效果图:
(4)\d 查找数字。
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>反斜线小写d</title>
- </head>
- <body>
- <script type="text/javascript">
- str='index and php 2 and Php and pHp 3 and indox and 20 indax and indBx andpcp and pp and p p and p-p and p_p and p\np and p9p';
- arr = str.match(/p\dp/ig)//查找数字。
- alert(arr);
- </script>
- </body>
- </html>
(5)\D 查找非数字字符。
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>反斜线大写D</title>
- </head>
- <body>
- <script type="text/javascript">
- str='index and php 2 and Php and pHp 3 and indox and 20 indax and indBx andpcp and pp and p p and p-p and p_p and p\np and p9p';
- arr = str.match(/p\Dp/ig)//查找非数字。
- alert(arr);
- </script>
- </body>
- </html>
(6)\s 查找空白字符。
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>反斜线小写s,查找空白字符。</title>
- </head>
- <body>
- <script type="text/javascript">
- str='index and php 2 and Php and pHp 3 and indox and 20 indax and indBx andpcp and pp and p p and p-p and p_p and p\np and p9p';
- arr = str.match(/p\sp/ig)//反斜线小写s,查找空白字符。
- alert(arr);
- </script>
- </body>
- </html>
(7)\S 查找非空白字符。
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>反斜线大写S,查找非空白字符。</title>
- </head>
- <body>
- <script type="text/javascript">
- str='index and php 2 and Php and pHp 3 and indox and 20 indax and indBx andpcp and pp and p p and p-p and p_p and p\np and p9p';
- arr = str.match(/p\Sp/ig)//反斜线大写S,查找非空白字符。
- alert(arr);
- </script>
- </body>
- </html>
(8)\b 匹配单词边界。
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>反斜线小写b,匹配单词边界。</title>
- </head>
- <body>
- <script type="text/javascript">
- str='index and php 2 and Php and pHp 3 and indox and 20 indax and indBx andpcp and pp and p p and p-p and p_p and p\np and p9p';
- arr = str.match(/\bp.p/ig)//反斜线小写b,匹配单词边界。
- alert(arr);
- </script>
- </body>
- </html>
(9)\B 匹配非单词边界。
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>反斜线大写B,匹配非单词边界。</title>
- </head>
- <body>
- <script type="text/javascript">
- str='index and php 2 and Php and pHp 3 and indox and 20 indax and indBx andpcp and pp and p p and p-p and p_p and p\np and p9p';
- arr = str.match(/\Bp.p/ig)//反斜线大写B,匹配非单词边界。
- alert(arr);
- </script>
- </body>
- </html>
javascript正则表达式——元字符的更多相关文章
- JavaScript —— 正则表达式元字符
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- JavaScript正则表达式详解(一)正则表达式入门
JavaScript正则表达式是很多JavaScript开发人员比较头疼的事情,也很多人不愿意学习,只是必要的时候上网查一下就可以啦~本文中详细的把JavaScript正则表达式的用法进行了列表,希望 ...
- JavaScript 正则表达式上——基本语法
定义 JavaScript种正则表达式有两种定义方式,定义一个匹配类似 <%XXX%> 的字符串 1. 构造函数 var reg=new RegExp('<%[^%>]+%&g ...
- JavaScript正则表达式(三)
正则表达式可以: •测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或一个信用卡号码模式.这称为数据有效性验证 •替换文本.可以在文档中使用一个正则表达式 ...
- javascript正则表达式简介
javascript正则表达式 javascript正则表达式 regular expression是一个描述字符模式的对象: ECMAScript中的RegExp类表示正则表达式: String ...
- 第一百零五节,JavaScript正则表达式
JavaScript正则表达式 学习要点: 1.什么是正则表达式 2.创建正则表达式 3.获取控制 4.常用的正则 假设用户需要在HTML表单中填写姓名.地址.出生日期等.那么在将表单提交到服务器进一 ...
- JavaScript正则表达式知识点
通过学习imooc课程<JavaScript正则表达式>http://www.imooc.com/video/12539,对视频教学内容做一个知识整理. 一个正则表达式在线工具:http: ...
- javascript 正则表达式补充
定义 JavaScript种正则表达式有两种定义方式,定义一个匹配类似 <%XXX%> 的字符串 1. 构造函数 var reg=new RegExp('<%[^%>]+%&g ...
- JavaScript 正则表达式基础语法
前言 正则表达式在人们的印象中可能是一堆无法理解的字符,但就是这些符号却实现了字符串的高效操作.通常的情况是,问题本身并不复杂,但没有正则表达式就成了大问题.javascript中的正则表达式作为相当 ...
随机推荐
- js(react.js) button click 事件无法触发
今天遇到一个诡异的问题.button 上的点击事件触发不了. 找个几个小时,原因是 js 报错了. <Button type="primary" htmlType=" ...
- Angular-cli新建项目目录结构详解
Angular-cli新建项目目录结构详解 在上一篇博客中我们已经通过Angular CLI命令行工具创建出来一个全新的Angular项目,要想写项目,首先我们要先搞清楚项目的目录结构是怎样的,每个文 ...
- leetcode539
public class Solution { public int FindMinDifference(IList<string> timePoints) { * ]; foreach ...
- PHP 取网页变量
$_POST["test"]; $_GET["test"];isset(); if(isset($_GET["yyuid"]))
- c++builder delphi 调用dll dll编写
c++builder动态调用dll // 定义 typedef int __stdcall MyFunction (int x, char *str); ; String dllName = &quo ...
- Color the ball (线段树的区间更新问题)
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但 ...
- 有一些sql 是必须要做笔记的!!
select CONCAT(unix_timestamp(),"-",id,"-",name) as aa,age from workers; //连接字段 s ...
- Java多线程-线程的调度(优先级)
与线程休眠类似,线程的优先级仍然无法保障线程的执行次序.只不过,优先级高的线程获取CPU资源的概率较大,优先级低的并非没机会执行. 线程的优先级用1-10之间的整数表示,数值越大优先级越高,默认的优先 ...
- Android中资源文件夹res/raw和assets的使用
Android中资源文件夹res/raw和assets的使用 2011-12-08 11:05 494人阅读 评论(0) 收藏 举报 androidxml存储stringencodinglayout ...
- 使用图形界面管理工具Navicat for MySQL连接Mysql数据库时提示错误:Can't connect to MySQL server (10060)
版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处,谢谢. https://blog.csdn.net/testcs_dn/article/details/ ...