admin管理员组文章数量:1203178
I want to replace only a part of the string of a regex pattern match. I found this answer but I don't get it...
How do I use substitution?
Example of what I want: keep the first slug digit, only replace others
/09/small_image/09x/
> /09/thumbnail/
1st: unknown digit
2nd: "small_image"
3rd: unknown digit + "x"
Here is what I have so far:
var regexPattern = /\/\d\/small\_image\/\d*x/;
var regexPattern = /\/\d\/(small\_image\/\d*x)$1/; ??
var result = regexPattern.test(str);
if (result) {
str = str.replace(regexPattern, 'thumbnail');
}
I want to replace only a part of the string of a regex pattern match. I found this answer but I don't get it...
How do I use substitution?
Example of what I want: keep the first slug digit, only replace others
/09/small_image/09x/
> /09/thumbnail/
1st: unknown digit
2nd: "small_image"
3rd: unknown digit + "x"
Here is what I have so far:
var regexPattern = /\/\d\/small\_image\/\d*x/;
var regexPattern = /\/\d\/(small\_image\/\d*x)$1/; ??
var result = regexPattern.test(str);
if (result) {
str = str.replace(regexPattern, 'thumbnail');
}
Share
Improve this question
edited May 23, 2017 at 11:48
CommunityBot
11 silver badge
asked Oct 10, 2016 at 3:07
user1775888user1775888
3,30314 gold badges49 silver badges67 bronze badges
4
|
2 Answers
Reset to default 19
var input = "/09/small_image/09x/";
var output = input.replace(/(\/\d+\/)small_image\/\d*x/, "$1thumbnail");
console.log(output);
Explanation:
Put the part you want to keep in parentheses, then refer to that as $1
in the replacement string - don't put $1
in your regex. So (\/\d+\/)
means to match a forward slash followed by one or more digits, followed by another forward slash.
(Note that you don't need to escape underscores in a regex.)
Go with
var regexPattern = /(\/\d+\/)small\_image\/\d*x/;
and
str = str.replace(regexPattern, '$1thumbnail');
First, you were missing the +
. Because 09
are two digits, you need the regexp to match one or more digits (\ḑ
would be exactly one). This is accomplished by \d+
Second, everything you match is being removed at first. To get the /09/
part back afterwards, you have to remember it by putting it into brackets in the regexp (
...)
and afterwards reference it in the replacement via $1
One could as well create other groups and reference them by $2
,$3
...
本文标签: javascriptregex replace only a part of the matchStack Overflow
版权声明:本文标题:javascript - regex replace only a part of the match - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738603252a2102195.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
09
and the09
in09x
must match? – user663031 Commented Oct 10, 2016 at 3:38$1
inside a regexp would mean? (Hint: it would mean the end of the string, followed by a1
.) (2) Why are you testing withRegExp.test
before replacing? (Hint: you don't need to, since the replacement would not happen anyway unless there was a match.) (3) Why are you escaping the underscore? (Hint: you don't need to.) – user663031 Commented Oct 10, 2016 at 3:54