前端Web前端之路让前端飞

javascript 正则表达式

2017-08-17  本文已影响9人  鸭梨山大哎

replace方法的参数可以使正则表达式

var str='apple Store';
console.log(str.replace(new RegExp(/store/i),'school'))//apple school

还可以写成

var str='apple Store';
console.log(str.replace(/store/i,'school'))//apple school

test方法

/apple/.test('apple store')//true
/apple/i.test('apple store')//true
new RegExp(/apple/i).test('orange')//false

exec方法

/apple/.exec('apple store sell apple')//["apple", index: 0, input: "apple store sell apple"]
/apple/.exec('orange')//null

可以用new或者直接创建正则表达式

var x=new RegExp(/apple/i)
x.constructor//function RegExp() { [native code] }
var x=/apple/i
x.constructor
function RegExp() { [native code] }

更多内容参见鸭梨山大哎

上一篇 下一篇

猜你喜欢

热点阅读