How can I convert this into an one-liner JavaScript regex?
html.replace(/src="(.*?)"/gi, function($1)
{ return $1.replace(/(abc.*?)\/abc/gi, "abc");
});
The above code block should be self-explanatory of what I'm trying to accomplish, but I'll elaborate.
What I'm trying to accomplish is replace all matches of this regex /src="(.*?)"/ which contains the substring: abc[random_characters]/abc to just abc. And so for example
src="abc[random_characters]/abc[random_characters]" => src="abc[random_characters]"
Edit: One-liner without the anonymous function call or callback.
Edit: Solution
html.replace(/src="abc(?:.*?)\/(abc[^"]+)"/gi, "src=\"$1\"");
Use backreference:
html.replace(/src="(abc[^\/]*\/?)+"/gi, 'src="$1"');
(abc[^\\/]*\\/?)
— matches abc + random characters (except for /
);
"$1"
— a backreference to the captured group.
However, I'm not quite sure it satisfies you requirements. Your description of a problem is a little bit inconsistent.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.