JS 正则表达式替换不起作用 [英] JS regex replace not working

查看:63
本文介绍了JS 正则表达式替换不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JS 字符串

I've got a JS string

var str = '<at id="11:12345678">@robot</at> ping'; 

我需要删除字符串的这一部分

I need to remove this part of a string

<at id="11:12345678">@

所以我正在尝试使用

var str = str.replace("<at.+@","");

但是执行后没有任何变化.此外,如果我尝试使用 ma​​tch 它会给我

But there is no change after excution. Moreover if I try to use match it gives me

str.match("<at.+@");
//Result from Chrome console Repl
["<at id="11:12345678">@", index: 0, input: "<at id="11:12345678">@robot</at> ping"]

所以模式确实有效,但替换什么都不做

So pattern actualy works but replace do nothing

推荐答案

Use // for regex.将 " 替换为 /.

Use // for regex. Replace "<at.+@" with /<at.+@/.

var str = '<at id="11:12345678">@robot</at> ping'; 

str = str.replace(/<at.+@/,"");

console.log(str);

替换<的文档/strong>

这篇关于JS 正则表达式替换不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆