JavaScript RegEx To capture URL between angle brackets (or pipe) regex capture between two strings,regex capture between quotes,regex

Consider the following URLs:

  • <http://www.google.com>
  • <http://www.google.com|www.google.com>
  • <http://google.com|google.com>

I'm trying to figure a RegEx that would capture the URL after < up until | OR >

I've tried URL.match(/<([^>|\|]+)/g) but it always capture the first <

Desired output is simply: http://www.google.com

Answer:1

The RegEx is correct. String#match will return the complete match set. You need to extract the first captured group.

Use RegExp#exec to get the URLs.

var str = `<http://www.google.com>
<http://www.google.com|www.google.com>
<http://google.com|google.com>`;

var regex = /<([^>|\|]+)/g;

var urls = [];
while(match = regex.exec(str)) {
    urls.push(match[1]); // Get first captured group, and push in array
}

console.log(urls);
document.body.innerHTML = '<pre>' + JSON.stringify(urls, 0, 4) + '</pre>';
Answer:2

This kind of pattern doesn't complex require regular expressions! You can use a simple pattern and string operations:

var url = "<http://www.google.com|www.google.com>";
var parts = url.replace(/^<|>$/, "").split("|");

For the solution using regex, try the following:

var url = "<http://www.google.com|www.google.com>";
var match = /(?:<|\|)([^>|]+)/g.exec(url);

You can then access the value of the first capturing group like this:

var url = match[1];

By calling exec on the same regular expression several times, you can find multiple matches (the multiple URLs you're looking for).

Explanation of the regular expression:

  • (?:<|\|) is a non-capturing group ((?: ... )) that looks for either a < or a | symbol at the beginning. (In your case, every URL will either have a < or a | on the left side of it!)
  • ([^>|]+) is a capturing group (( ... )) that capturing a sequence of characters that are not > or |. You don't need to escape the | within a character class, it only has special meaning outside of it.
Answer:3
str.match(/(http\:.*)(?=\|)/)[0]

var strs = ["<http://www.google.com>",
"<http://www.google.com|www.google.com>",
"<http://google.com|google.com>"];

strs.forEach(function(str) {
  // if `str` contains `|` character,
  // match characters that are followed by `|`
  if (/\|/.test(str)) {
    console.log(str.match(/(http\:.*)(?=\|)/)[0])
  } 
  // else match characters that are not `<`, `>`
  else {
    console.log(str.match(/[^<>]+/)[0])
  }
})
Answer:4

I'm working on a voxel engine that uses WebGL directly. The textures look great within a voxel-engine game (which uses three.js), but look horrible in my engine. Here's an example with a simple grass ...

I'm working on a voxel engine that uses WebGL directly. The textures look great within a voxel-engine game (which uses three.js), but look horrible in my engine. Here's an example with a simple grass ...

Can someone explain why these array methods in Javascript have functions as parameters? An example would be: newArray = oldArray.map( function(val){ return val + 3; }); also this, array.sort(...

Can someone explain why these array methods in Javascript have functions as parameters? An example would be: newArray = oldArray.map( function(val){ return val + 3; }); also this, array.sort(...

I have a simple form here, <?php echo form_open('moneyexchange/invest_first_page'); ?> <input id="amount" value="" type="text" placeholder="Amount in &euro;" name="writtenamount"/&...

I have a simple form here, <?php echo form_open('moneyexchange/invest_first_page'); ?> <input id="amount" value="" type="text" placeholder="Amount in &euro;" name="writtenamount"/&...

I have two sortables connected by means of jQuery sortable plugin. Is it possible to change target for draggables for one of them? I want to drag not from #sortable1 to #sortable2, but to $('#sortable'...

I have two sortables connected by means of jQuery sortable plugin. Is it possible to change target for draggables for one of them? I want to drag not from #sortable1 to #sortable2, but to $('#sortable'...

  1. jquery sortable connected lists
  2. jquery sortable connectwith
  3. jquery connectedsortable refresh
  4. jquery ui sortable connected lists
  5. jquery sortable connectwith multiple
  6. jquery ui connect sortable with droppable
  7. jquery ui connected sortable
  8. jquery ui connectedsortable style