[英]replacement using regex expression in javascript
I have an HTML string in java which has data within certain attributes 我在Java中有一个HTML字符串,其中的某些属性中包含数据
One example being 一个例子是
<img class="show-grid" src="http://localhost:4502/content/dam/original" alt="4.7-products.jpg">
Now, the problem that I have is I want to append some string at the end of the string which is within src attribute (string starting with http and ending with original here) which is the url. 现在,我遇到的问题是我想在src属性(以http开头并以此处以original开头的字符串)内的字符串末尾添加一些字符串。
Only thing I know: 我唯一知道的是:
http
. 字符串始终以http
开头。 Somethings I don't know 我不知道的事
I tried with following regex: 我尝试了以下正则表达式:
target.search(/(\bhttp.*\"\b)/gi)
The idea is to find to all strings: 这个想法是找到所有字符串:
http
and end with "
. 以http
开头,以"
结尾。 So, I thought this regex will give me the following string: 因此,我认为此正则表达式将为我提供以下字符串:
http://localhost:4502/content/dam/original"
But instead it gives me: 但是相反,它给了我:
http://localhost:4502/content/dam/original" alt="
Any idea why it goes beyond the current word, Is it because it looks for space between words and then when it finds "
in the space seperated new word it stops its search there . 知道为什么它超出了当前单词,是不是因为它在单词之间寻找空格,然后当它在空格分开的新单词中找到"
时"
就停止了在此处的搜索。
How can I modify target.search(/(\\bhttp.*\\"\\b)/gi)
to get the desired result there? 如何修改target.search(/(\\bhttp.*\\"\\b)/gi)
以获得所需的结果?
Just split and add back in the character you need with a positive lookaround 只需拆分即可,并以积极的眼神重新添加所需的角色
String string = "http://yourtestinthstsdfasdfasdfhttp://moreteststrings";
String[] parts = string.split("(?=http)");
String part1 = parts[0]; // is http://yourtestinthstsdfasdfasdf
String part2 = parts[1]; // is http://moreteststrings
Your regex are "greedy" (see : 您的正则表达式是“贪婪的”(请参阅: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/RegExp https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/RegExp https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp ). https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp )。 To change it, modify /(\\bhttp.*\\"\\b)/
to /(\\bhttp.*?\\"\\b)/
. 要对其进行更改, /(\\bhttp.*\\"\\b)/
/(\\bhttp.*?\\"\\b)/
修改为/(\\bhttp.*?\\"\\b)/
。
Edit : This still does not work, see my message below 编辑:这仍然行不通,请参阅下面的消息
That regex is using the default greedy modifiers. 该正则表达式使用默认的贪婪修饰符。 This means that it is searching for as many *
(any characters) as it can find. 这意味着它将搜索尽可能多的 *
(任何字符)。 Instead, what you are looking for is the *?
相反,您要查找的是*?
(zero or more, non-greedy) modifier, which changes this behaviour. (零个或多个,非贪婪的)修饰符,可更改此行为。
Your new regex search would look like: 您的新正则表达式搜索如下所示:
target.search(/http.*?\"/gi)
/http.*(?=\\"\\s)/gi
would match anything from http until the end quote followed by a white space. It uses (?=) which is called Positive Lookahead. /http.*(?=\\"\\s)/gi
将匹配从http到结束引号的所有内容,后跟一个空格,它使用(?=),称为正向超前。
A different approach, if you just want to match anything inside the src attribute without need to speculate if http string exists: src="([^"]*)
. 如果您只想匹配src属性中的任何内容而无需推测是否存在http字符串,则采用另一种方法: src="([^"]*)
。
You can play with it https://regex101.com/r/rH0yZ2/3 . 您可以使用它https://regex101.com/r/rH0yZ2/3 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.