Reputation: 19651
My Text
1618148163#@#JASSER-PC#-#1125015374#@#anas kayyat#-#1543243035#@#anas kayyat#-#
Result Should Be:
JASSER-PC
anas kayyat
anas kayyat
I am using :
(?<=#@#)(.+)(?=#-#)
But it gives me that :
JASSER-PC#-#1125015374#@#anas kayyat#-#1543243035#@#anas kayyat
Upvotes: 0
Views: 558
Reputation: 47183
I'll give you a non-regex answer, since using regular expressions isn't always appropriate, be it speed or readibility of the regex itself:
function getText(text) {
var arr = text.split("#@#"); // arr now contains [1618148163,JASSER-PC#-#1125015374,anas kayyat#-#1543243035,anas kayyat#-#]
var newarr = [];
for(var i = 0; i < arr.length; i++) {
var index = arr[i].indexOf("#-#");
if(index != -1) { // if an array element doesn't contain "#-#", we ignore it
newarr.push(arr[i].substring(0, index));
}
}
return newarr;
}
Now, using
getText("1618148163#@#JASSER-PC#-#1125015374#@#anas kayyat#-#1543243035#@#anas kayyat#-#");
returns what you wanted.
Upvotes: 0
Reputation: 816262
JavaScript does not support lookbehinds. Make the quantifier non greedy, and use:
var regex = /#@#(.+?)#-#/g;
var strings = [];
var result;
while ((result = regex.exec(input)) != null) {
strings.push(result[1]);
}
Upvotes: 0
Reputation: 10762
The group (.+)
will match as much as it can (it's "greedy"). To make it find a minimal match you can use (.+?)
.
Upvotes: 0
Reputation: 655129
JavaScript’s regular expressions don’t support look-behind assertions (i.e. (?<=…)
and (?<!…)
), so you can’t use that regular expression. But you can use this:
#@#(.+)(?=#-#)
Then just take the matched string of the first group. Additionally, to only match as little as possible, make the +
quantifier non-greedy by using +?
.
Upvotes: 4