我正在尝试构建一个正则表达式,它以逗号分隔列表中的最后一个逗号后返回所有内容。
// So if my list is as followed
var tag_string = "red, green, blue";
// Then my match function would return
var last_tag = tag_string.match(A_REGEX_I_CANNOT_FIGURE_OUT_YET);
// Then in last tag I should have access to blue
// I have tried the following things:
var last_tag = tag_string.match(",\*");
// I have seen similar solutions, but I cannot figure out how to get the only the last string after the last comma.
答案 0 :(得分:7)
您可以尝试以下内容:
var last_tag = tag_string.match("[^,]+$").trim();
首先获取" blue"
然后删除尾随空格。
答案 1 :(得分:3)
答案 2 :(得分:2)
[^,]+$
似乎可以解决问题。它与blue
匹配。
答案 3 :(得分:-1)
tag_string.match(/[^,\s]+$/)
=> [ 'blue', index: 12, input: 'red, green, blue' ]
多数民众赞成:)