[英]Visual Studio Code. Regex to search/replace just the apostrophe
I'm trying to find whatever starts with a word, followed by a '
, and then followed by a word( /\\w'\\w/
). 我试图找到以单词开头,然后是'
,然后是单词( /\\w'\\w/
)的内容。 For example(i'm, it's, what's,there's,John's), Take John's for example, it will match n's
. 例如(我是,什么是,有约翰),以约翰为例,它将匹配n's
。 That is perfect, but once that it is matched I want to replace just the '
and not n
and s
. 那是完美的,但是一旦匹配,我只想替换'
而不是n
和s
。
You can use capturing group. 您可以使用捕获组。
([^'])'([^'])
| |________ Group 2
|
|______________ Group 1
let arr = [`i'm`, `it's`, `what's`,`there's`,`John's`] arr.forEach(e=>{ e = e.replace(/([^'])'([^'])/g, "$1$2") console.log(e) })
Ctrl+f
add regex pattern and select .*
option. 按Ctrl+f
添加正则表达式模式,然后选择.*
选项。 $1$2
在第二个输入框中添加替换值$1$2
Yes use capture groups. 是的,使用捕获组。 Let's assume you want to replace the ' with "" 假设您要用“”替换“
Match RegExp: 匹配正则表达式:
(\w)'(\w)
Replace with "": 用。。。来代替 ””:
$1""$2
Here's an alternative that doesn't use capturing groups, instead using a lookbehind and a lookahead: 这是一种不使用捕获组的替代方法,而是使用先行查找和先行查找:
const replaced = "John's it's I'm 'not this'".replace(/(?<=\\w)'(?=\\w)/g, ''); console.log(replaced);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.