I have a string like this:
var variable = 'one|two|three|four|five';
This regex /^(\w+)\|/
matches first word (from left side). In other word, $1
returns one
. Now I need to get second word from the right side (four
).
EX1:
var variable = 'one|two|three';
I want two
.
EX2:
var variable = 'one|two|three|four';
I want three
.
EX3:
var variable = 'one|two|three|four|five|six|seven';
I want six
.
Always I want second word from the right side. How can I do that?
I have a string like this:
var variable = 'one|two|three|four|five';
This regex /^(\w+)\|/
matches first word (from left side). In other word, $1
returns one
. Now I need to get second word from the right side (four
).
EX1:
var variable = 'one|two|three';
I want two
.
EX2:
var variable = 'one|two|three|four';
I want three
.
EX3:
var variable = 'one|two|three|four|five|six|seven';
I want six
.
Always I want second word from the right side. How can I do that?
Share Improve this question asked Feb 19, 2016 at 21:25 stackstack 10.2k22 gold badges70 silver badges130 bronze badges 11- Let's see your Javascript code as well. – MortenMoulder Commented Feb 19, 2016 at 21:27
-
2
str.match(/\|(\w+)\|\w+$/)[1]
. In JS, there is no other way. – Wiktor Stribiżew Commented Feb 19, 2016 at 21:28 -
@Snorlax What? Actually I need to check
if(variable.test(/second-word-form-right/) == 'four') { return true; }
– stack Commented Feb 19, 2016 at 21:29 -
What about
one|two
? Would you wantone
? – Brendan Abel Commented Feb 19, 2016 at 21:30 - @WiktorStribiżew Yep ..... – stack Commented Feb 19, 2016 at 21:31
5 Answers
Reset to default 2Try this:
/\w+(?=\|\w+$)/
var regexp = /\w+(?=\|\w+$)/;
document.write(`<pre>
${[
'one|two|three',
'one|two|three|four',
'one|two|three|four|five',
'one|two|three|four|five|six'
].map(input=>`${input}=>${regexp.exec(input)[0]}`).join('\n')}
</pre>`)
If that doesn't convince you, here's a link to regex 101 demonstrating it as well.
If a lookahead is too slow for you, you can also try:
/(\w+)\|\w+$/
which still does the same thing, except now the result is stored in a group instead of returned directly, so change regexp.exec(input)[0]
to regexp.exec(input)[1]
:
var regexp = /(\w+)\|\w+$/;
document.write(`<pre>
${[
'one|two|three',
'one|two|three|four',
'one|two|three|four|five',
'one|two|three|four|five|six'
].map(input=>`${input}=>${regexp.exec(input)[1]}`).join('\n')}
</pre>`)
Alternatively, if you want to do this with pure regular expressions, you can actually do this:
(?:(.*)\|){2}
Check out the matched group on this: http://regexr./3crfr
Then for Javascript (or any other language), simply extract the group. You could do that like this:
var variable = 'one|two|three|four';
console.log(/(?:(.*)\|){2}/g.exec(variable)[1]);
Works fine, yeah?
Ignoring the regular expression, you can do this:
var variable = 'one|two|three|four';
var result = variable.split("|");
console.log(result[result.length - 2]);
If you want to do it with a regular expression, you can do this:
var variable = 'one|two|three|four';
console.log(variable.match(/\|(\w+)\|\w+$/)[1]);
Either way works fine.
An iterative approach , beginning at the end of input string. At first |
character begin inner loop, at second |
character break outer loop
var variable = "one|two|three|four|five"
, i = variable.length
, match = [];
while (true) {
if (variable[i] === "|") {
while (variable[--i] !== "|") {
match.push(variable[i]);
}
match = match.reverse().join("");
break;
}
--i
};
console.log(match)
Or, you could use the EOL anchor like this [^|]+(?=\|[^|]+$)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745612882a4636055.html
评论列表(0条)