最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How do I trace string from right-side in regex? - Stack Overflow

matteradmin5PV0评论

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 want one? – Brendan Abel Commented Feb 19, 2016 at 21:30
  • @WiktorStribiżew Yep ..... – stack Commented Feb 19, 2016 at 21:31
 |  Show 6 more ments

5 Answers 5

Reset to default 2

Try 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 [^|]+(?=\|[^|]+$)

Post a comment

comment list (0)

  1. No comments so far