简体   繁体   English

替换RegEx模式中的特定字符

[英]Replace specific character within RegEx pattern

How do I replace specific characters that are within a RegEx pattern? 如何替换RegEx模式中的特定字符?

Let's say I have a string git@github.com:foo-bar/baz . 假设我有一个字符串git@github.com:foo-bar/baz

What I want to do is replace that colon with a / character. 我想做的是用/字符代替冒号。

var gitUrl = 'git@github.com:foo-bar/baz';
gitUrl.replace(/(github.com|bitbucket.org):/, '/'); // I want to keep github.com/bitbucket.org!

Just use a simple String.replace with String , as Regex is not required. 只需将简单的String.replaceString ,因为不需要Regex。

gitUrl = gitUrl.replace(":", "/");

If you really want it, you would do use the captured group by $1 如果确实需要,则可以使用捕获的组$1

gitUrl = gitUrl.replace(/(github.com|bitbucket.org):/, "$1/");

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

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