[英]javascript regular expression replace 3digitis from path string
I have a <img>
where I need to change two digits from the path. 我有一个
<img>
,需要从路径中更改两位数。 I have no idea in how to write regular expressions. 我不知道如何编写正则表达式。
<img src="/cms/ig/immopix/100/9165c73366c8830058b0f95c0fcacef5_100.jpg" />
^ ^
| |
------------------------------------
|
replace those "1" in "6"
so it looks like this
------------------------------------
| |
<img src="/cms/ig/immopix/600/9165c73366c8830058b0f95c0fcacef5_600.jpg" />
The img name is an md5 value which could also contain "100", so I cannot just replace every "100". img名称是一个md5值,它也可以包含“ 100”,因此我不能仅替换每个“ 100”。 How can I do this?
我怎样才能做到这一点?
So there are a few img tags in DOM which I loop like this 所以在DOM中有一些img标签,我像这样循环
$('.object-overview-wrap').each(function(i){
var img = $(this).children('.img-wrap').children('img');
console.log(img.attr('src'));
var imgSrc = img.attr('src');
img.attr('src', '/cms/ig/immopix/600/88f9f658ed1d506ce36b28565b79405f_600.jpg');
});
The console.log gives me: /cms/ig/immopix/100/9165c73366c8830058b0f95c0fcacef5_100.jpg which I would like to replace, but I don't know how. console.log给了我:/cms/ig/immopix/100/9165c73366c8830058b0f95c0fcacef5_100.jpg我想替换,但是我不知道如何。
Help and links explaining how to write and use regular expressions are much appreciated! 非常感谢帮助和解释如何编写和使用正则表达式的链接!
You could replace "/100/"
with "/600/"
and then "_100."
您可以将
"/100/"
替换为"/600/"
,然后"_100."
with "_600."
与
"_600."
. 。
Regexes could of course work here, however it's simpler this way. 正则表达式当然可以在这里工作,但是这种方式更简单。
This should work for your case: 这应该适合您的情况:
var s='<img src="/cms/ig/immopix/100/9165c73366c8830058b0f95c0fcacef5_100.jpg" />';
var r = s.replace(/100\b/g, "600");
//=> <img src="/cms/ig/immopix/600/9165c73366c8830058b0f95c0fcacef5_600.jpg" />
Or to make more strict: 或更严格地说:
var r = s.replace(/([/_])100\b/g, "$1600");
//=> <img src="/cms/ig/immopix/600/9165c73366c8830058b0f95c0fcacef5_600.jpg" />
My approach would be: Avoid regex. 我的方法是:避免使用正则表达式。 Use maybe this:
使用可能是这样的:
var mystring = srcString.split('/');
mystring[4]=600;
mystring[5] = mystring[5].split('_')[0] + '_' + '600.jpg';
mystring.join('/');
Or similar. 或类似。 Using
split
and join
is fun. 使用
split
和join
很有趣。 But a simple replace works here, too 但是简单的替换也可以在这里工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.