如何使用正则表达式替换子字符串中的特定字符?

How to replace a specific character in a substring using regex?

我的objective

这个问题是关于Visual Studio代码搜索框的。

我想用 mystringmy substring with spaces 中的字符“_”替换所有“空格”

例如:

-> 来自

{
    "Show_details": mystring("my substring with spaces"),
    "Type_of_event": mystring("my substring with spaces2"),
}

-> 到

{
    "Show_details": mystring("my_substring_with_spaces"),
    "Type_of_event": mystring("my_substring_with_spaces2"),
}

我试过的

我只设法得到 `my_substring_with_spaces`。但我不明白如何进一步进行。
Search: mystring\("([a-z]*?( )[a-z]*?)"\)

您可以使用

(?<=mystring\("[^"]*)\s+(?=[^"]*"\))

详情

  • (?<=mystring\("[^"]*) - 紧靠左侧,必须有 mystring(",然后是 "
  • 以外的 0 个或更多字符
  • \s+ - 一个或多个空格(如果必须将两个连续的空格转换为两个连续的 _,则移除 +
  • (?=[^"]*"\)) - 紧靠右边,除 " 外必须有 0 个或更多字符,然后是 ").

见截图: