JavaScript How do I trace string from right-side in regex?

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.com/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?

Answer:1

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>`)
Answer:2

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.

Answer:3

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)
Answer:4

Say I have an array of strings like this: originalArray = ["some value", "another value", "and another"] How would you add to both the beginning and end of each string so it looks like this: ...

Say I have an array of strings like this: originalArray = ["some value", "another value", "and another"] How would you add to both the beginning and end of each string so it looks like this: ...

  1. append values javascript
  2. append values javascript array

I want to run mongod command in node.js using Promises, so that database operations can run only after mongodb process is started. I tried my hands with following, but failed: var Promise = require("...

I want to run mongod command in node.js using Promises, so that database operations can run only after mongodb process is started. I tried my hands with following, but failed: var Promise = require("...

  1. node.js mongodb exec

Problem: I'm trying to validate when the user only inputted special characters without typing any number with it. I'm using this expression /^\+?[0-9 \.-]+$/ to accept only '+' sign, numbers, dots, ...

Problem: I'm trying to validate when the user only inputted special characters without typing any number with it. I'm using this expression /^\+?[0-9 \.-]+$/ to accept only '+' sign, numbers, dots, ...

This is what I thought should work but it doesn't. I'm am using unicode 112 for the "p" key and 115 for "s". var audio = document.getElementById("lessonTrack"); window.addEventListener("...

This is what I thought should work but it doesn't. I'm am using unicode 112 for the "p" key and 115 for "s". var audio = document.getElementById("lessonTrack"); window.addEventListener("...

  1. bind keyboard shortcut javascript
  2. bind keyboard shortcut
  3. bind keyboard shortcut to function
  4. bind keyboard command
  5. ubuntu bind keyboard shortcut
  6. windows bind keyboard shortcut
  7. wpf bind keyboard shortcut
  8. jquery bind keyboard shortcuts
  9. wxpython bind keyboard shortcuts
  10. key bind keyboard shortcuts
  11. bind applescript to keyboard shortcut
  12. windows 7 bind keyboard shortcut
  13. c# bind keyboard shortcuts