简体   繁体   English

JS Regex:删除单词后的任何内容(仅)

[英]JS Regex: Remove anything (ONLY) after a word

I want to remove all of the symbols (The symbol depends on what I select at the time) after each word, without knowing what the word could be.我想删除每个单词后的所有符号(符号取决于我当时选择的内容),但不知道该单词可能是什么。 But leave them in before each word.但是在每个单词之前把它们留在里面。

A couple of examples:几个例子:

!!hello! my! !!name!!! is !!bob!! should return...应该回...

!!hello my !!name is !!bob ; !!hello my !!name is !!bob ; for !为了!

and

$remove$ the$ targetted$@ $$symbol$$@ only $after$ a $word$ should return... $remove$ the$ targetted$@ $$symbol$$@ only $after$ a $word$应该返回...

$remove the targetted@ $$symbol@ only $after a $word ; $remove the targetted@ $$symbol@ only $after a $word ; for $$

You need to use capture groups and replace:您需要使用捕获组并替换:

"!!hello! my! !!name!!! is !!bob!!".replace(/([a-zA-Z]+)(!+)/g, '$1');

Which works for your test string.这适用于您的测试字符串。 To work for any generic character or group of characters:为任何通用字符或字符组工作:

var stripTrailing = trail => {
  let regex = new RegExp(`([a-zA-Z0-9]+)(${trail}+)`, 'g');
  return str => str.replace(regex, '$1');
};

Note that this fails on any characters that have meaning in a regular expression: []{}+*^$.请注意,这对任何在正则表达式中有意义的字符都会失败:[]{}+*^$。 etc. Escaping those programmatically is left as an exercise for the reader.等。以编程方式逃避这些留给读者作为练习。

UPDATE更新

Per your comment I thought an explanation might help you, so:根据您的评论,我认为解释可能对您有所帮助,因此:

First, there's no way in this case to replace only part of a match, you have to replace the entire match.首先,在这种情况下无法仅替换匹配的一部分,您必须替换整个匹配。 So we need to find a pattern that matches, split it into the part we want to keep and the part we don't, and replace the whole match with the part of it we want to keep.所以我们需要找到一个匹配的模式,把它分成我们想要保留的部分和我们不保留的部分,然后用我们想要保留的部分替换整个匹配。 So let's break up my regex above into multiple lines to see what's going on:所以让我们把上面的正则表达式分解成多行,看看发生了什么:

First we want to match any number of sequential alphanumeric characters, that would be the 'word' to strip the trailing symbol from:首先,我们要匹配任意数量的连续字母数字字符,这将是要从中去除尾随符号的“单词”:

(       // denotes capturing group for the 'word'
  [     // [] means 'match any character listed inside brackets'
    a-z // list of alpha character a-z
    A-Z // same as above but capitalized
    0-9 // list of digits 0 to 9
  ]+    // plus means one or more times
)

The capturing group means we want to have access to just that part of the match.捕获组手段,我们希望能够访问只是比赛的一部分。 Then we have another group然后我们还有另一组

(
  ! // I used ES6's string interpolation to insert the arg here
  + // match that exclamation (or whatever) one or more times
)

Then we add the g flag so the replace will happen for every match in the target string, without the flag it returns after the first match.然后我们添加g标志,以便对目标字符串中的每个匹配进行替换,而不会在第一次匹配后返回标志。 JavaScript provides a convenient shorthand for accessing the capturing groups in the form of automatically interpolated symbols, the '$1' above means 'insert contents of the first capture group here in this string'. JavaScript 以自动内插符号的形式为访问捕获组提供了方便的速记,上面的“$1”表示“在此字符串中插入第一个捕获组的内容”。

So, in the above, if you replaced '$1' with '$1$2' you'd see the same string you started with, if you did 'foo$2' you'd see foo in place of every word trailed by one or more !, etc.所以,在上面,如果你用 '$1$2' 替换 '$1' 你会看到你开始的相同的字符串,如果你做了 'foo$2' 你会看到 foo 代替每个词尾随一个或多个!, 等等。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM