用Angular.js中的正则表达式替换HTTP协议

时间:2014-11-25 07:37:25

标签: regex angularjs

我在java脚本中使用正则表达式,只想将具有(JPEG,.PNG)扩展名的图像URL的HTTP://替换为单个字符串。 像这是一个URL

http://static01.nyt.com/images/2014/11/24/us/MARION1/MARION1-master675.jpg

并希望转换为

哎/ static01.nyt.com /图像/十一分之二千零十四/ 24 / US / MARION1 / MARION1-master675.jpg

但不应替换网址

http://static01.nyt.com/images/2014/11/24/us/MARION

我在替换函数中使用此语法,但它替换了所有URL

var res = str.replace("http:/\//g", "hey");

谢谢,

1 个答案:

答案 0 :(得分:2)

要进行全局替换,您必须添加g修饰符。 \b匹配单词字符和非单词字符之间存在的边界。

str.replace(/\bhttp:\//g, "hey")

示例:

> var s = "http://static01.nyt.com/images/2014/11/24/us/MARION1/MARION1-master675.jpg http://static01.nyt.com/images/2014/11/24/us/MARION1/MARION1-master675.jpg"
undefined
> s.replace(/\bhttp:\//g, "hey")
'hey/static01.nyt.com/images/2014/11/24/us/MARION1/MARION1-master675.jpg hey/static01.nyt.com/images/2014/11/24/us/MARION1/MARION1-master675.jpg'