如何删除两个特定字符之间的子字符串 [英] How to remove a substring between two specific characters

查看:310
本文介绍了如何删除两个特定字符之间的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个字符串:

"this is the beginning, this is what i want to remove/ and this is the end"

如何使用Javascript来定位逗号和正斜杠之间的字符串?
(我也想删除逗号和斜杠)

How do I use Javascript to target the string between the comma and the forward slash? (I also want to remove the comma and the slash)

推荐答案

string = string.replace( /,.*?\//, '' );

regexp 匹配自身,。*?匹配任何字符序列(由于修饰符而优先选择最短匹配)和 \ / 匹配斜杠。 (斜杠需要使用反斜杠进行转义,因为它也用作正则表达式分隔符。不幸的是,JavaScript不支持替代regexp文字分隔符,因此您只需要处理倾斜牙签综合征。)

In the regexp, , matches itself, .*? matches any sequence of characters (preferring the shortest match, due to the ? modifier) and \/ matches a slash. (The slash needs to be escaped with a backslash because it's also used as the regexp delimiter. Unfortunately, JavaScript doesn't support alternative regexp literal delimiters, so you'll just have to cope with the leaning toothpick syndrome.)

请注意,上面的代码会删除在字符串中的斜杠后面第一个逗号到第一个。如果可能有多个逗号或斜杠,则根据您希望发生的情况,有多种方法可以修改正则表达式的行为。例如:

Note that the code above removes everything from the first comma to the first following slash in the string. Depending on what you want to happen if there might be multiple commas or slashes, there are various ways to modify the behavior of the regexp. For example:


  • .replace(/,。* \ _,'')删除从第一个逗号到 last 斜杠的所有内容;

  • .replace(/ ^ (。*),。*?\ //,'$ 1') last 逗号中删除所有内容,然后斜线到下一个斜杠;

  • .replace(/^(.*),.*\//,'$ 1')删除 last中的所有内容逗号,然后斜线到字符串中的 last 斜杠;

  • .replace(/,[^ \\ \\ /] * \ //,'')与原始正则表达式相同,即删除从第一个逗号到第一个斜杠后的所有内容(即使中间还有其他逗号);

  • .replace(/,[^,\ /] * \ //,'')删除第一个子字符串以逗号开头并以斜杠结尾,之间不包含任何逗号或斜杠;

  • .replace(/ ^ (。*),[^,\ /] * \ //, $ 1’ )除去的最后子串以逗号开始,并且以斜线结束,并且没有逗号或之间斜杠中;和

  • .replace(/ ^,[^,\ /] * \ // g,'')删除所有以逗号开头并以斜杠结尾的子字符串,中间没有逗号或斜杠。

  • .replace( /,.*\//, '' ) removes everything from the first comma to the last slash;
  • .replace( /^(.*),.*?\//, '$1' ) removes everything from the last comma followed by a slash up to the next slash;
  • .replace( /^(.*),.*\//, '$1' ) removes everything from the last comma followed by a slash up to the last slash in the string;
  • .replace( /,[^\/]*\//, '' ) does the same as the original regexp, i.e. removes everything from the first comma to the first following slash (even if there are other commas in between);
  • .replace( /,[^,\/]*\//, '' ) removes the first substring that begins with a comma and ends with a slash, and does not contain any commas or slashes in between;
  • .replace( /^(.*),[^,\/]*\//, '$1' ) removes the last substring that begins with a comma and ends with a slash, and has no commas or slashes in between; and
  • .replace( /^,[^,\/]*\//g, '' ) removes all substrings that begin with a comma and end with a slash, and have no commas or slashes in between.

另请注意,由于regexp语法的历史怪癖,元字符实际上将匹配任何字符,除了换行符( \\\
)。大多数regexp实现提供了一些方法来关闭这个怪癖并使真正匹配所有字符,但JavaScript由于某种原因没有。如果你的字符串可能包含换行符,你应该在上面的正则表达式中用一些。。 javascript-regex-multiline-flag-doesnt-work>解决方法,例如 [\\\ S] (适用于大多数PCRE样式的regexp引擎) )或 [^] (更短,但特定于JavaScript的正则表达式风格)。

Also note that, due to a historical quirk of regexp syntax, the . metacharacter will actually match any character except a newline ("\n"). Most regexp implementations provide some way to turn off this quirk and make . really match all characters, but JavaScript, for some reason, doesn't. If your string might contain newlines, you should replace all occurrences of . in the regexps above with some workaround such as [\s\S] (works in most PCRE-style regexp engines) or [^] (shorter, but specific to JavaScript's regexp flavor).

这篇关于如何删除两个特定字符之间的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆