简体   繁体   English

如何使用正则表达式或纯 JavaScript 从字符串中删除所有括号及其内容并保留其他文本格式

[英]How to remove all parenthesis and their contents from a string using a regular expression or pure JavaScript and retain other text format

Currently I am entering text inside a text area and on clicking the submit button of my form, I am applying some changes to the text mainly using.replaceAll();目前我正在文本区域内输入文本,并在单击表单的提交按钮时,我主要使用.replaceAll(); 对文本应用一些更改; js methods and printing it out as a paragraph inside a div. js 方法并将其作为 div 中的段落打印出来。

If I enter text that already has newlines as well as parenthesis into the text area, I want my script to strip/remove all parenthesis as well as the contents inside parenthesis AND keep all newlines as they are.如果我在文本区域中输入已经有换行符和括号的文本,我希望我的脚本剥离/删除所有括号以及括号内的内容,并保持所有换行符不变。

I have tried the following regex + js function with the sample text however there are some formatting issues that get introduced (some newlines are removed and text following parenthesis ends up on the same line as the text in the previous line where parenthesis was removed)我已经尝试了以下正则表达式 + js function 和示例文本,但是引入了一些格式问题(删除了一些换行符,括号后面的文本与删除括号的前一行中的文本位于同一行)

regex+js code:正则表达式+js代码:

updatedText = updatedText.replaceAll(/\s*\(.*?\)\s*/g, '');

Sample input:样本输入:

06.abbaa(bbbbadf)
01.lijer      ttt
06.foo(sample)dddddddddd(garbage)
06.text(lksdhfljkld)20001(kkkkkksssss)
01.asdfg
13.fffffffff(fdsgggg/asdfasdf)
03.ggggggg(defg/abc)
18.abc     123abc
blahblah
qqqqqq
anon
06.barbarbar
10.foobar
18.fooistbar
12.blahhhh(moreblah/blah)
15.2035number(test)(test0/2test)
12.testing morewords
03.random(blahhh)(blahahaha/morerandomstuff)
17.anotherrandomtextstring
11.string
testy(mctest)
isTestingStrings
11.string
11.string
11.string

the result outputted where lines are combined when they are not suppose to be is this:在不应该合并行时输出的结果是:

06.abbaa01.lijer ttt
06.foodddddddddd06.text2000101.asdfg
13.fffffffff03.ggggggg18.abc 123abc
blahblah
qqqqqq
anon
06.barbarbar
10.foobar
18.fooistbar
12.blahhhh15.2035number12.testing morewords
03.random17.anotherrandomtextstring
11.string
testyisTestingStrings
11.string
11.string
11.string
  • for example "testy" and "isTestingStrings" should be on 2 separate lines.例如,“testy”和“isTestingStrings”应该在 2 个单独的行上。
  • each line could have any number of(0 or more) "(xxxx...)" parenthesis with content and in these cases should also retain the newlines.每行可以有任意数量的(0 个或更多)“(xxxx ...)”括号和内容,在这些情况下也应该保留换行符。

Why does replacing the matched text with an empty string result in the newline character being removed from the string, and how can I resolve this issue in my script.为什么用空字符串替换匹配的文本会导致从字符串中删除换行符,以及如何在我的脚本中解决此问题。 Is there better regex to handle this or would this require writing a custom js parser?是否有更好的正则表达式来处理这个问题,或者这是否需要编写一个自定义的 js 解析器?

Have you tried the following?您是否尝试过以下操作?

inputString.replace(/\([^)]*\)/g,"")

 s=`06.abbaa(bbbbadf) 01.lijer ttt 06.foo(sample)dddddddddd(garbage) 06.text(lksdhfljkld)20001(kkkkkksssss) 01.asdfg 13.fffffffff(fdsgggg/asdfasdf) 03.ggggggg(defg/abc) 18.abc 123abc blahblah qqqqqq anon 06.barbarbar 10.foobar 18.fooistbar 12.blahhhh(moreblah/blah) 15.2035number(test)(test0/2test) 12.testing morewords 03.random(blahhh)(blahahaha/morerandomstuff) 17.anotherrandomtextstring 11.string testy(mctest) isTestingStrings 11.string 11.string 11.string`; console.log(s.replace(/\([^)]*\)/g,""))

The reason why new newline characters are being removed from your string is that newline characters are considered spacing characters, and are matched by the two \s in your regular expression.从字符串中删除新换行符的原因是换行符被视为空格字符,并且与正则表达式中的两个\s匹配。 If you want to keep newlines but remove whitespaces and tabs before and after parentheses, change \s to [\t ] :如果要保留换行符但要删除括号前后的空格和制表符,请将\s更改为[\t ]

updatedText = updatedText.replaceAll(/[\t ]*\(.*?\)[\t ]*/g, '');

If you never meant to remove spacing characters and you just want to remove parentheses along with the text inside then, just remove the \s* sequences from your regex:如果您从未打算删除空格字符,而只想删除括号以及其中的文本,则只需从您的正则表达式中删除\s*序列:

updatedText = updatedText.replaceAll(/\(.*?\)/g, '');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅从Javascript中捕获正则表达式的括号中获取内容 - Get only contents from capturing parenthesis of a Regular Expression in Javascript 如何使用JavaScript正则表达式删除所有'#'符号(如果它们在单词开头) - How to remove all '#' symbols if they are at the start of word using javascript regular expression JavaScript:如何使用正则表达式从字符串中删除空行? - JavaScript: how to use a regular expression to remove blank lines from a string? 如何使用纯JavaScript从子节点中删除所有类? - how to remove all class from child node using pure javascript? 如何在Wordpress,PHP,JavaScript上使用正则表达式从字符串(URL)中删除特殊字符 - How to remove special characters from a string (URL) using regular expression on wordpress, php, javascript 如何在JavaScript中将输入括号与正则表达式匹配? - How to match a input parenthesis with regular expression in JavaScript? 从JavaScript中的字符串中删除括号 - Remove Parenthesis from String in Javascript 如何使用javascript正则表达式从括号中分别获取所有文本 - How to get all the text separately from the bracket using javascript regular expression 如何在javascript中替换或删除括号内的所有文本字符串 - how to replace or remove all text strings outside of parenthesis in javascript 如何使用正则表达式从字符串地址获取特定文本? - How to Get Specific text from string address using regular expression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM