正方形括号内的正则表达匹配方括号Google电子表格

时间:2021-02-28 09:47:14

I am currently scripting using Google Script. I'm trying to select anything that isn't the characters within the square bracket by writing:

我目前正在使用Google Script编写脚本。我正在尝试通过编写以下内容来选择方括号内不是字符的任何内容:

var cleantext = text.replace(/[^\s\w"!,、。\.??!:]/g,'');

var cleantext = text.replace(/ [^ \ s \ w“!,,。\。??!:] / g,'');

I want to also keep "[" and "]" and I have followed some of the tutorials here trying "\\]" and "\\["

我还要保留“[”和“]”,我已经按照这里的一些教程尝试“\\]”和“\\ [”

var cleantext = text.replace(/[^\s\w"!,、。\.??!:"\\]""\\["]/g,'');

var cleantext = text.replace(/ [^ \ s \ w“!,,。\。??!:”\\]“”\\ [“] / g,'');

or trying \\] and \\[

或尝试\\]和\\ [

var cleantext = text.replace(/[^\s\w"!,、。\.??!:\\]\\[]/g,'');

var cleantext = text.replace(/ [^ \ s \ w“!,,。\。??!:\\] \\ [] / g,'');

Please feel free to change how my question is worded, as I am finding that I probably don't know what question I'm actually trying to ask here as there are many similar questions with similar titles already here at *.

请随意改变我的问题的措辞,因为我发现我可能不知道我在这里问的是什么问题,因为在*上有很多类似的问题。

I wish to edit a whole column of cells, which are a combination of Japanese, Chinese, and English characters.

我想编辑一整列单元格,它们是日文,中文和英文字符的组合。

For an example: "こんにちは、私はJimです???? | [Audio.Category:Jim]" would output to: "こんにちは、私はJimです [Audio.Category:Jim]"

例如:“こんにちは,私はJimです????| [Audio.Category:Jim]”将输出到:“こんにちは,私はJimです[Audio.Category:Jim]”

Deleting emojis, and other characters not defined by what's within the brackets.

删除表情符号以及括号内未定义的其他字符。

1 个解决方案

#1


2  

To include ] and [ into a JavaScript regex character class, you need to escape ] and you do not have to escape [:

要包含]和[进入JavaScript正则表达式字符类,你需要转义]并且你不必转义[:

/[abc[\]xyz]/
     ^^^

If you need to support ASCII letters and Japanese only, you need to add the Japanese letter ranges:

如果您需要支持ASCII字母和日语,则需要添加日语字母范围:

/[^\s"!,、。.??!:[\][A-Za-z\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFFEF\u4E00-\u9FAF\u2605-\u2606\u2190-\u2195\u203B]+/g

Here is a sample solution:

这是一个示例解决方案:

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var cell = sheet.getRange('F13').getValue();
  Logger.log(cell);
  var reg_ascii_letter = "A-Za-z";
  var reg_japanese_letter = "\\u3000-\\u303F\\u3040-\\u309F\\u30A0-\\u30FF\\uFF00-\\uFFEF\\u4E00-\\u9FAF\\u2605-\\u2606\\u2190-\\u2195\\u203B";
  var rx = new RegExp("[^\\s\"!,、。.??!:[\\][" + reg_ascii_letter + reg_japanese_letter + "]+", "g");
  Logger.log(rx);
  var nval = cell.replace(rx, '').replace(/(\s){2,}/g, '$1');
  sheet.getRange('F15').setValue(nval);
}

正方形括号内的正则表达匹配方括号Google电子表格

In a similar way, you may build a Unicode regex for any letter.

以类似的方式,您可以为任何字母构建Unicode正则表达式。

#1


2  

To include ] and [ into a JavaScript regex character class, you need to escape ] and you do not have to escape [:

要包含]和[进入JavaScript正则表达式字符类,你需要转义]并且你不必转义[:

/[abc[\]xyz]/
     ^^^

If you need to support ASCII letters and Japanese only, you need to add the Japanese letter ranges:

如果您需要支持ASCII字母和日语,则需要添加日语字母范围:

/[^\s"!,、。.??!:[\][A-Za-z\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFFEF\u4E00-\u9FAF\u2605-\u2606\u2190-\u2195\u203B]+/g

Here is a sample solution:

这是一个示例解决方案:

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var cell = sheet.getRange('F13').getValue();
  Logger.log(cell);
  var reg_ascii_letter = "A-Za-z";
  var reg_japanese_letter = "\\u3000-\\u303F\\u3040-\\u309F\\u30A0-\\u30FF\\uFF00-\\uFFEF\\u4E00-\\u9FAF\\u2605-\\u2606\\u2190-\\u2195\\u203B";
  var rx = new RegExp("[^\\s\"!,、。.??!:[\\][" + reg_ascii_letter + reg_japanese_letter + "]+", "g");
  Logger.log(rx);
  var nval = cell.replace(rx, '').replace(/(\s){2,}/g, '$1');
  sheet.getRange('F15').setValue(nval);
}

正方形括号内的正则表达匹配方括号Google电子表格

In a similar way, you may build a Unicode regex for any letter.

以类似的方式,您可以为任何字母构建Unicode正则表达式。