简体   繁体   中英

Replace specific character within RegEx pattern

How do I replace specific characters that are within a RegEx pattern?

Let's say I have a string 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.

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

If you really want it, you would do use the captured group by $1

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

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.

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