Reputation: 135
How can I separate my string by regex match()? I want use only jQuery RegEx.
var MyStr = 'BeginStr ABCDEF EndStr' // The result should: ABCDEF
How can I separate "ABCDEF"?
Below is a solution, but I like to improve it, how can I eliminate the function replace()? I want use only one time the function match().
var MyStr = 'BeginStr ABCDEF EndStr'; // The result should: ABCDEF
sRegEx = /BeginStr.*?(?=EndStr)/;
var sResult = String(MyStr.match(sRegEx)); // It results: BeginStr ABCDEF
var sMenuPoint = String(MyStr.match(sRegEx)).replace(/BeginStr/, ''); // It results: ABCDEF
alert(sResult);
Thanks in advance, Sandro.
Upvotes: 0
Views: 103
Reputation: 39355
Using simple replace()
function will do it for you along with group capturing($1
, $2
, etc):
sResult = MyStr.replace(/.*BeginStr(.*?)(?=EndStr).*/, "$1");
or
sResult = MyStr.replace(/.*BeginStr(.*?)EndStr.*/, "$1");
Upvotes: 2