javascript 正则表达式

作者: 鸭梨山大哎 | 来源:发表于2017-08-17 16:56 被阅读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] }

更多内容参见鸭梨山大哎

相关文章

网友评论

    本文标题:javascript 正则表达式

    本文链接:https://www.haomeiwen.com/subject/yxrnrxtx.html