javascript 如何删除两个特定字符之间的子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12582164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:35:11  来源:igfitidea点击:

How to remove a substring between two specific characters

javascriptregexsubstring

提问by bumpy

So I have a string:

所以我有一个字符串:

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

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)

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

回答by Ilmari Karonen

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

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.)

regexp 中,匹配自身,.*?匹配任何字符序列(由于?修饰符,首选最短匹配)并\/匹配斜杠。(斜杠需要用反斜杠转义,因为它也用作正则表达式分隔符。不幸的是,JavaScript 不支持替代正则表达式文字分隔符,所以你只需要应对倾斜牙签综合症。)

Note that the code above removes everything from the firstcomma to the firstfollowing 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( /,.*\//, '' )removes everything from the firstcomma to the lastslash;
  • .replace( /^(.*),.*?\//, '$1' )removes everything from the lastcomma followed by a slash up to the next slash;
  • .replace( /^(.*),.*\//, '$1' )removes everything from the lastcomma followed by a slash up to the lastslash 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 lastsubstring that begins with a comma and ends with a slash, and has no commas or slashes in between; and
  • .replace( /^,[^,\/]*\//g, '' )removes allsubstrings that begin with a comma and end with a slash, and have no commas or slashes in between.
  • .replace( /,.*\//, '' )删除从第一个逗号到最后一个斜杠的所有内容;
  • .replace( /^(.*),.*?\//, '$1' )删除从最后一个逗号后跟斜杠到下一个斜杠的所有内容;
  • .replace( /^(.*),.*\//, '$1' )删除从最后一个逗号后跟斜杠到字符串中最后一个斜杠的所有内容;
  • .replace( /,[^\/]*\//, '' )与原始正则表达式相同,即删除从第一个逗号到第一个斜杠之间的所有内容(即使中间还有其他逗号);
  • .replace( /,[^,\/]*\//, '' )删除以逗号开头并以斜杠结尾的第一个子字符串,并且中间不包含任何逗号或斜杠
  • .replace( /^(.*),[^,\/]*\//, '$1' )删除以逗号开头并以斜杠结尾的最后一个子字符串,并且中间没有逗号或斜杠;和
  • .replace( /^,[^,\/]*\//g, '' )删除所有以逗号开头并以斜杠结尾且中间没有逗号或斜杠的子字符串。

Also note that, due to a historical quirk of regexp syntax, the .metacharacter will actually match any character excepta 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 workaroundsuch as [\s\S](works in most PCRE-style regexp engines) or [^](shorter, but specific to JavaScript's regexp flavor).

另请注意,由于正则表达式语法的历史怪癖,.元字符实际上将匹配换行符 ( "\n")之外的任何字符。大多数 regexp 实现提供了一些方法来关闭这个怪癖并使.真正匹配所有字符,但 JavaScript,出于某种原因,没有。如果您的字符串可能包含换行符,您应该.使用一些变通方法替换上述正则[\s\S]表达式中出现的所有 ,例如(适用于大多数 PCRE 样式的正则表达式引擎)或[^](较短,但特定于 JavaScript 的正则表达式风格)。

回答by Bergi

As your question is tagged with regex, you seem to want .replace:

由于您的问题用正则表达式标记,您似乎想要.replace

return str.replace(/, this is what i want to remove\//, "");

If you don't know the string in between, use

如果您不知道中间的字符串,请使用

/,(.+?)\//

or with a *instead of the +if the string could also be empty

或者用一个*代替+如果字符串也可以为空

With string functions, it would be

使用字符串函数,它将是

var cpos = str.indexOf(","),
    spos = str.indexOf("/");
if (cpos > -1 && spos > cpos)
    return str.substr(0, cpos)+str.substr(spos+1);

回答by Frederik.L

return str.split(',')[1].split('/')[0];

回答by Apurv

var commaIndex=str.indexOf(',')
var slashIndex=str.indexOf('/')
var finalString=str.substring(commaIndex,slashIndex)

Hope it will work

希望它会起作用