简体   繁体   English

如何用正则表达式替换两个斜杠之间的文本?

[英]how to replace text between 2 slashes with regex?

I have a src attribute that I only want to replace whatever dimensions happen to be between b1/ and /imglibrary : b1/200x300/imglibrary 我有一个src属性,我只想替换b1//imglibrary之间的任何尺寸: b1/200x300/imglibrary

<img class="Img" src="b1/200x300/imglibrary" alt=""/>
var $image = $('.Img').attr('src');
var editedImage = $image.replace(/b1\/.*?\/imglibrary/,"b1/800x600/imglibrary");
console.log(editedImage);

but the output is: /imglibrary/b1/800x600/imglibrary/ 但输出为: /imglibrary/b1/800x600/imglibrary/

I can't simply find and replace the text 200x300 because that text will change based on the image. 我不能简单地找到并替换200x300文本,因为该文本会根据图像而变化。 Can anyone please shed some light on where I am going wrong? 谁能告诉我我要去哪里错了?

Assuming, that you want to replace resolution value, which pattern looks like numberxnumber , use following RegExp : 假设您要替换分辨率值,该模式类似于numberxnumber ,请使用以下RegExp

 var $image = $('.Img').attr('src'); var editedImage = $image.replace(/(?!\\/)\\d+x\\d+(?=\\/)/, "800x600"); console.log(editedImage); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <img class="Img" src="b1/200x300/imglibrary" alt=""/> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM