Javascript 如何使用正则表达式获取特定字符后的所有字符,例如逗号(“,”)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4058923/
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-08-23 07:18:29  来源:igfitidea点击:

How can I use regex to get all the characters after a specific character, e.g. comma (",")

javascriptregex

提问by user357034

Need a Regex to get all characters after ,(not including it) from a variable. This variable can contain for example

需要一个正则表达式,从变量中获取(不包括它)之后的所有字符。这个变量可以包含例如

'SELECT___100E___7',24
'SELECT___100E___7',1
'SELECT___100E___7',286
'SELECT___100E___7',5147

Note: There can be any length of characters after the ,in this variable.

注意:,此变量中的之后可以有任意长度的字符。

An explanation of the regexp would be a added help for the novice :)

对正则表达式的解释将是对新手的额外帮助:)

Edit: a javascript answer would be just as good

编辑:javascript 答案同样好

回答by HoLyVieR

You don't need regex to do this. Here's an example :

你不需要正则表达式来做到这一点。这是一个例子:

var str = "'SELECT___100E___7',24";
var afterComma = str.substr(str.indexOf(",") + 1); // Contains 24 //

回答by PleaseStand

Short answer

简答

Either:

任何一个:

  • ,[\s\S]*$or ,.*$to match everything after the first comma (see explanation for which one to use); or

  • [^,]*$to match everything after the last comma (which is probably what you want).

  • ,[\s\S]*$,.*$匹配第一个逗号之后的所有内容(请参阅使用哪个的解释);或者

  • [^,]*$匹配最后一个逗号之后的所有内容(这可能是您想要的)。

You can use, for example, /[^,]*/.exec(s)[0]in JavaScript, where sis the original string. If you wanted to use multiline mode and find all matches that way, you could use s.match(/[^,]*/mg)to get an array (if you have more than one of your posted example lines in the variable on separate lines).

例如,您可以/[^,]*/.exec(s)[0]在 JavaScript 中使用,其中s是原始字符串。如果您想使用多行模式并以这种方式查找所有匹配项,您可以使用s.match(/[^,]*/mg)获取一个数组(如果您在不同行的变量中有多个已发布的示例行)。

Explanation

解释

  • [\s\S]is a character class that matches both whitespace and non-whitespace characters (i.e. all of them). This is different from .in that it matches newlines.
  • [^,]is a negated character class that matches everything except for commas.
  • *means that the previous item can repeat 0 or more times.
  • $is the anchor that requires that the end of the match be at the end of the string (or end of line if using the /m multiline flag).
  • [\s\S]是匹配空白字符和非空白字符(即所有字符)的字符类。这与.它匹配换行符不同。
  • [^,]是一个否定字符类,匹配除逗号之外的所有内容。
  • *表示前一项可以重复 0 次或多次。
  • $是要求匹配结尾在字符串结尾(如果使用 /m 多行标志,则在行结尾)的锚点。

For the first match, the first regex finds the first comma ,and then matches all characters afterward until the end of line [\s\S]*$, including commas.

对于第一次匹配,第一个正则表达式找到第一个逗号,,然后匹配之后的所有字符,直到行尾[\s\S]*$,包括逗号。

The second regex matches as many non-comma characters as possible before the end of line. Thus, the entire match will be after the last comma.

第二个正则表达式在行尾匹配尽可能多的非逗号字符。因此,整个匹配将在最后一个逗号之后。

回答by Sven Marnach

[^,]*$

might do. (Matches everything after the last comma).

可能做。(匹配最后一个逗号之后的所有内容)。

Explanation: [^,]matches every character except for ,. The *denotes that the regexp matches any number of repetition of [^,]. The $sign matches the end of the line.

说明:[^,]匹配除,. 的*表示正则表达式的任何数量的重复匹配[^,]。该$标志线的末端匹配。

回答by darioo

.+,(.+)

Explanation:

解释:

.+,

will search for everything before the comma, including the comma.

将搜索逗号之前的所有内容,包括逗号。

(.+) 

will search for everything after the comma, and depending on your regex environment,

将搜索逗号后的所有内容,具体取决于您的正则表达式环境,


is the reference for the first parentheses captured group that you need, in this example, everything after the comma.

是您需要的第一个括号捕获组的引用,在本例中,逗号之后的所有内容。

回答by Patricio Córdova

This matches a word from any length:

这匹配任何长度的单词:

var phrase = "an important number comes after this: 123456";
var word = "this: ";
var number = phrase.substr(phrase.indexOf(word) + word.length);
// number = 123456

回答by deepesh

Maybe you can try this

也许你可以试试这个

var str = "'SELECT___100E___7',24";
    var res = str.split(',').pop();

回答by Bakhtawar GIll

You can try with the following:

您可以尝试以下操作:

new_string = your_string.split(',').pop().trim();

This is how it works:

这是它的工作原理:

  • split(',')creates an array made of the different parts of your_string
  • split(',')创建一个由不同部分组成的数组 your_string

(e.g. if the string is "'SELECT___100E___7',24", the array would be ["'SELECT___100E___7'", "24"]).

(例如,如果字符串为"'SELECT___100E___7',24",则数组为["'SELECT___100E___7'", "24"])。

  • pop()gets the last element of the array
  • pop()获取数组的最后一个元素

(in the example, it would be "24").

(在示例中,它将是"24")。

This would already be enough, but in case there might be some spaces (not in the case of the OP, but more in general), we could have:

这已经足够了,但是如果可能有一些空格(不是在 OP 的情况下,而是在一般情况下),我们可以:

  • trim()that would remove the spaces around the string (in case it would be " 24 ", it would become simply "24")
  • trim()这将删除字符串周围的空格(如果是" 24 ",它会变得简单"24"

It's a simple solution and surely easier than a regexp.

这是一个简单的解决方案,肯定比正则表达式更容易。

回答by Romain Linsolas

Another idea is to do myVar.split(',')[1];

另一个想法是做 myVar.split(',')[1];

For simple case, not using a regexp is a good idea...

对于简单的情况,不使用正则表达式是个好主意......

回答by Alex Rashkov

This should work

这应该工作

preg_match_all('@.*\,(.*)@', '{{your data}}', $arr, PREG_PATTERN_ORDER);

You can test it here: http://www.spaweditor.com/scripts/regex/index.php

你可以在这里测试:http: //www.spaweditor.com/scripts/regex/index.php

RegEx: .*\,(.*)

正则表达式: .*\,(.*)

Same RegEx test here for JavaScript: http://www.regular-expressions.info/javascriptexample.html

此处针对 JavaScript 进行相同的 RegEx 测试:http: //www.regular-expressions.info/javascriptexample.html