Reputation: 281
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]
Upvotes: 1
Views: 81
Reputation: 1600
"category~[330526|330519]^Size{1}~[m]".replace(/(category~[\d+\|\d+]).*/,"$1"), you should get the string, or you can use match as well.
Upvotes: 0
Reputation: 14937
Yet another approach...
var test = 'category~[330526|330519]^Size{1}~[m]';
var result = test.replace(/\^.+/,"");
Upvotes: 0
Reputation: 67928
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.
Upvotes: 0
Reputation: 318352
var test = 'category~[330526|330519]^Size{1}~[m]';
var result = test.split('^').shift();
Upvotes: 3