JS正则表达式比较常见用法

网络编程 发布日期:2024/10/5 浏览次数:1

正在浏览:JS正则表达式比较常见用法

废话不多说,直接步入正轨了,想要了解js正则表达式用法的相关知识,通过本教程学习吧。

定义和使用

var patt1 = new RegExp("hello"); 
var patt2 = /world/ ; 

test方法

test() 方法检索字符串中的指定值。返回值是 true 或 false。

var pat = /my/; 
var str = "this is my code..."; 
console.log(pat.test(str)); // true 

exec方法

exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。

var pat = /hello/; 
console.log(pat.exec("oh hello world")); //返还hello 

正则表达式类型

/pattern/attributes
参数 attributes 是一个可选的字符串,常用属性 “g”、”i” ,分别用于指定全局匹配、区分大小写的匹配。

var str = "Visit Hunger"; 
var patt1 = /hunger/i; //不区分大小写 
console.log(str.match(patt1)); //全局匹配 
var str="hello hunger valley! I am hunger"; 
var patt1=/hunger/g; 
console.log(str.match(patt1)); //不区分大小写,全局匹配 
var str="hello Hunger valley! I am hunger"; 
var patt1=/hunger/gi; 
console.log(str.match(patt1)); 

字符串正则

1. search

字符串查找

var str="Visit W3School!"; 
console.log(str.search(/w3school/)); //-1 console.log(str.serach(/w3school/i)); // 6 

2. match

字符串匹配

var str="1 plus 2 equal 33"; 
console.log(str.match(/\d+/)); //[1] 
console.log(str.match(/\d+/g)); //[1,2,33] 

3. replace

字符串替换

var str="Hello JI! oh I am hunger" 
console.log(str.replace(/Hunger/, "valley")); console.log(str.replace(/hunger/ig, "hunger")); 

4.split

字符串分割

var str = "Hello Hunger , oh I am Hunger"; 
str.split("");
str.split(/\s+/); 

正则写法

"htmlcode">

var str="Is this all there is"; 
var patt1=/[a-h]/g;
console.log(str.match(patt1)); 

"htmlcode">

var str="hello jikexueyuan!"; 
var patt1=/[^jike]/g; 
console.log(str.match(patt1)); 

"htmlcode">

var str="hello hunger! How are you"; 
var patt1=/hello|you/g; c
onsole.log(str.match(patt1)); 

"htmlcode">

var str="That's hot!"; 
var patt1=/h.t/g; 
document.write(str.match(patt1)); 

"htmlcode">

var str="Give 100%!"; 
var patt1=/\w/g; 
document.write(str.match(patt1));

"htmlcode">

var str="Give 100%!"; var patt1=/\W/g; document.write(str.match(patt1)); 

"htmlcode">

var str="Give 100%!"; 
var patt1=/\d/g; 
document.write(str.match(patt1)); 

"htmlcode">

var str="Give 100%!"; var patt1=/\D/g; document.write(str.match(patt1)); 

"htmlcode">

var str="Is this all there is";
var patt1=/\s/g; document.write(str.match(patt1)); 

"htmlcode">

var str="Is this all there is"; var patt1=/\S/g; document.write(str.match(patt1)); 

"htmlcode">

var str="Hello jikexueyuan"; 
var patt1=/\bjikexueyuan/g; 
document.write(str.match(patt1)); 

"htmlcode">

var str="Hello Hunger.\n be a FE."; 
var patt1=/\n/g; document.write(str.search(patt1)); 

"htmlcode">

var str="Hello HHunger! Hello World!"; 
var patt1=/H+/g; 
document.write(str.match(patt1)); 
var str="Hello Hunger! Hello World!"; 
var patt1=/\w+/g; document.write(str.match(patt1)); 

"htmlcode">

var str="Hellooo Hunger! Hello World!"; var patt1=/lo*/g; document.write(str.match(patt1)) 

"htmlcode">

var str="1, 100 or 1000"; var patt1=/10"htmlcode">
var str="100, 1000 or 10000"; var patt1=/\d{4}/g; document.write(str.match(patt1)); 

"htmlcode">

var str="100, 1000 or 10000"; var patt1=/\d{3,4}/g; document.write(str.match(patt1)); 

"htmlcode">

var str="100, 1000 or 10000"; var patt1=/\d{3,}/g; document.write(str.match(patt1)); 

"htmlcode">

var str="Is this his"; var patt1=/is$/g; document.write(str.match(patt1)); 

"htmlcode">

var str="Is this his"; var patt1=/^Is/g; document.write(str.match(patt1)); 

常见正则

汉字: [\u4e00-\u9fa5]
手机号: 1[0-9]{10}
邮箱: (\S)+[@]{1}(\S)+[.]{1}(\w)+

几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。