-2

I want to extract "stringtoextract" below I can't see why it doesn't work: https://jsfiddle.net/dwbnj6z7/

  let url = 'https://mystuff.com/test/varstring/stringtoextract?id=test3'
  let regex = /https:\/\/stuff.com\/test\/varstring\/.+\/(.+)?.+/
  var match = regex.exec(url);
  alert(match[1]); 
2
  • 1
    Use let regex = /https:\/\/mydomain\.com\/test\/varstring\/([^?\/]+)/ Commented Feb 4, 2021 at 18:53
  • Better, use const segments = new URL(url).pathname.split('/'); console.log(segments.pop()); Commented Feb 4, 2021 at 19:03

1 Answer 1

1

Try out this code. I've changed the regex a bit:

let url = 'https://example.com/test/varstring/stringtoextract?id=test3'
let regex = /https:\/\/example\.com\/test\/varstring\/(.+)\?.+/
var match = regex.exec(url);
console.log(match[1]); 

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.