I have the following variable:
var test = category~[330526|330519]^Size{1}~[m]
How do I match just to get category~[330526|330519]
using regex.
This value can also change so it could be category~[3303226|333219]
只需尝试:
test.split('^')[0];
var test = 'category~[330526|330519]^Size{1}~[m]';
var result = test.split('^').shift();
你可以;
result = test.substr(0, test.indexOf("]") +1);
应该这样做:
category~\[\d+\|\d+\]
If you insist on using a Regex, this one doesn't care about what the category is (.*~\\[\\d+\\|\\d+\\])
. Here is a Rubular to prove it . But I have to say, the answer by @hsz is really the most insightful. The split
is probably the right tool for the job.
Yet another approach...
var test = 'category~[330526|330519]^Size{1}~[m]';
var result = test.replace(/\^.+/,"");
“ category〜[330526 | 330519] ^ Size {1}〜[m]”。replace(/(category〜[\\ d + \\ | \\\\ d +])。* /,“ $ 1”),应该获取字符串,或者您也可以使用match。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.