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

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

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

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

Share Improve this question edited Sep 13, 2017 at 4:04 tvanfosson 532k102 gold badges699 silver badges798 bronze badges asked Oct 30, 2010 at 13:37 user357034user357034 11k19 gold badges59 silver badges74 bronze badges 5
  • 1 Which language are you using? If you want a Java example, a Perl answer for example would confuse you. – justintime Commented Oct 30, 2010 at 13:43
  • javascript/jQuery sorry forgot to mention. So something like this var result_val = $(this).attr('href').match(RegEX); – user357034 Commented Oct 30, 2010 at 13:45
  • 1 Why do you absolutely need regex ? This solution can easily be implement with basic string manipulation. – HoLyVieR Commented Oct 30, 2010 at 14:07
  • 7 Meta-comment: If you come here from a google search, you are looking for Regex help, somewhat annoying to find lots of answers saying 'you don't need regex' – Andrew Jon Dodds Commented Nov 8, 2018 at 8:09
  • +up to what Andrew said – Coder Commented Jul 19, 2021 at 7:06
Add a comment  | 

10 Answers 10

Reset to default 121

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 //

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

You can use, for example, /[^,]*/.exec(s)[0] in JavaScript, where s is 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).

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

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.

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.

[^,]*$

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.

.+,(.+)

Explanation:

.+,

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

(.+) 

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

\1

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

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

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

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

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

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

  • pop() gets the last element of the array

(in the example, it would be "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:

  • trim() that would remove the spaces around the string (in case it would be " 24 ", it would become simply "24")

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

Maybe you can try this

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

(?<=,).+

A positive lookbehind on ,

The group is followed by any character . with a quantifier + of one or more of them.

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

RegEx: .*\,(.*)

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1736835657a3914075.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信