匹配所有包含在括号内的双引号的正则表达式:

huangapple go评论69阅读模式
英文:

regEx to match all double quotes wrapped in brackets

问题

需要匹配花括号{}之间的所有双引号,然后对这些双引号进行转义。

(37, "2012 Fall", null, null, 0, 1, "1420", {\"canDelete\":false, \"cantDeleteModes\":[2, 3, 5]}, "2020-05-28T18:06:48.000Z", "2020-10-27T19:42:03.000Z", 1, 1);

希望这有所帮助。

英文:

Looking for some help on this one. I need to match all double quotes between {} brackets. Then I will escape these double quotes.

(37, "2012 Fall", null, null, 0, 1, "1420", {"canDelete":false, "cantDeleteModes":[2, 3, 5]}, "2020-05-28T18:06:48.000Z", "2020-10-27T19:42:03.000Z", 1, 1);

Here is the reqex I have so far...

/(?<=\{).*?(?=\})/g

but that matches everything between the {} brackets.

Expected output...

(37, "2012 Fall", null, null, 0, 1, "1420", {\"canDelete\":false, \"cantDeleteModes\":[2, 3, 5]}, "2020-05-28T18:06:48.000Z", "2020-10-27T19:42:03.000Z", 1, 1);

Any help would be appreciated ;=)

答案1

得分: 1

这段代码应该可以让你开始:

    const input = `(37, "2012 Fall", null, null, 0, 1, "1420", {"canDelete":false, "cantDeleteModes":[2, 3, 5]}, "2020-05-28T18:06:48.000Z", "2020-10-27T19:42:03.000Z", 1, 1);`

    const regex = /{(.*)}/

    const substr = input.match(regex)[1]
    const replacement = substr.replaceAll('"', '\\"')

    const result = input.replace(substr, replacement)

    console.log(result)

Explanation

  • /{(.*)}/ 匹配花括号{}之间的所有内容(非贪婪模式)
  • input.match(regex)[1] - 返回第一个匹配的字符串

Remarks:
这对我来说是最简单且易理解的解决方案。如果你需要更高效的解决方案,可能一种可能的方法是遍历字符串的字符,注意任何 {} 括号,并仅在在看到开始括号但没有闭合括号时替换引号(并跟踪多个开始和闭合括号,以便注意开始位置)。因此,你需要一种“级别”计数器,该计数器根据打开和关闭括号的出现而增加和减少。

英文:

This code should get you going:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const input = `(37, &quot;2012 Fall&quot;, null, null, 0, 1, &quot;1420&quot;, {&quot;canDelete&quot;:false, &quot;cantDeleteModes&quot;:[2, 3, 5]}, &quot;2020-05-28T18:06:48.000Z&quot;, &quot;2020-10-27T19:42:03.000Z&quot;, 1, 1);`

const regex = /{(.*)}/

const substr = input.match(regex)[1]
const replacement = substr.replaceAll(&#39;&quot;&#39;, &#39;\\&quot;&#39;)

const result = input.replace(substr, replacement)

console.log(result)

<!-- end snippet -->

Explanation<br>

  • /{(.*)}/ matches everything between brackets {} non-lazily
  • input.match(regex)[1] - returns the string of the first match

Remarks:<br>
It was the easiest solution for me to write and to understand. If you need something more performant, one possible way of going about it would be to iterate over the chars of the string, be aware of any {} brackets and replace the quotes only when an opening bracket but no closing bracket was seen before (and keep track of multiple opening and closing brackets so to note the beginning. So you'd need some kind of "level"-counter which increases and decreases depending on the occurrence of opening and closing brackets).

答案2

得分: 0

如果您想匹配canDelete和cantDeleteModes,您可以使用正则表达式以外的更好方法

const str = `(37, "2012 Fall", null, null, 0, 1, "1420", {"canDelete":false, "cantDeleteModes":[2, 3, 5]}, "2020-05-28T18:06:48.000Z", "2020-10-27T19:42:03.000Z", 1, 1)`
const arr = JSON.parse(`[${str.slice(1,-1)}]`)
console.log(arr)
console.log(Object.entries(arr[7])); // 使用Object.keys只获取键或使用Object.values只获取值
英文:

If you want to match canDelete and cantDeleteModes you can do better than regex

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const str = `(37, &quot;2012 Fall&quot;, null, null, 0, 1, &quot;1420&quot;, {&quot;canDelete&quot;:false, &quot;cantDeleteModes&quot;:[2, 3, 5]}, &quot;2020-05-28T18:06:48.000Z&quot;, &quot;2020-10-27T19:42:03.000Z&quot;, 1, 1)`
const arr = JSON.parse(`[${str.slice(1,-1)}]`)
console.log(arr)
console.log(Object.entries(arr[7])); // use Object.keys to just get the key or Object.values to just get the values

<!-- end snippet -->

答案3

得分: 0

解析JSON应该使用JSON.parse,但是关于正则表达式,您可以使用正则表达式断言和RegExp::exec来逐步搜索字符串。请注意,我们在匹配中包括引号,因为我们希望在下一个exec中跳过它们。额外的正则表达式组帮助我们获取结果而不包括引号。

但是,这个解决方案不够健壮,因为如果您将花括号放在字符串内部,它可能会失败。如果您的源字符串不符合JSON格式,那么更好的选择是编写自己的解析器。

以下是您提供的代码的翻译部分:

const str = '(37, "2012 Fall", null, null, 0, 1, "1420", {"canDelete":false, "cantDeleteModes":[2, 3, 5]}, "2020-05-28T18:06:48.000Z", "2020-10-27T19:42:03.000Z", 1, 1, {"cantDelete":false, "cantDeleteModes":[2, 3, 5]})';

const regexp = new RegExp('(?<={^}*)&quot;([^&quot;]+)&quot;', 'g');

const matches = [];
let match;
while(match = regexp.exec(str)){
  matches.push(match[1]);
}

console.log(matches);

请注意,这只是您提供的代码的翻译部分,没有其他内容。

英文:

Parsing JSON should be done with JSON.parse but regarding regexps you could use regexp assertions and RegExp::exec to incrementally search strings. Notice that we include the quotes in the match since we want to skip them in the next exec. The extra regexp group help us to get the result without the quotes.

But the solution is brittle since if you put the curly brackets inside your string it could fail. If your source string doesn't comply JSON then the better option is to write your own parser.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const str = &#39;(37, &quot;2012 Fall&quot;, null, null, 0, 1, &quot;1420&quot;, {&quot;canDelete&quot;:false, &quot;cantDeleteModes&quot;:[2, 3, 5]}, &quot;2020-05-28T18:06:48.000Z&quot;, &quot;2020-10-27T19:42:03.000Z&quot;, 1, 1, {&quot;cantDelete&quot;:false, &quot;cantDeleteModes&quot;:[2, 3, 5]});&#39;;

const regexp = new RegExp(&#39;(?&lt;=\{[^}]*)&quot;([^&quot;]+)&quot;&#39;, &#39;g&#39;);

const matches = [];
let match;
while(match = regexp.exec(str)){
  matches.push(match[1]);
}

console.log(matches);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月8日 21:49:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定