[英]Regular Expression for last value in comma separated list
我正在嘗試構建一個正則表達式,它返回逗號分隔列表中最后一個逗號后的所有內容。
// 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.
您可以嘗試以下方式:
var last_tag = tag_string.match("[^,]+$").trim();
這將首先獲得" blue"
然后刪除尾隨空格。
[^,]+$
似乎可以做到這一點。 它匹配blue
。
tag_string.match(/[^,\s]+$/)
=> [ 'blue', index: 12, input: 'red, green, blue' ]
而已 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.