简体   繁体   中英

Regex - Get everything after, but not including, this character

I have an Id String which is appended to the end of the current pages URL, like so:

www.website.com?id=XXXXXXXXXXXX

I want to get the Id from the end of the URL, so I want to end up with just

XXXXXXXXXXXX

At the moment I have this:

var the_url;
var the_id;

the_id = the_url.replace(/^[^=]+/,"");

console.log(the_id);

But that is giving me this:

=XXXXXXXXXXXX

How do I get everything after the equals sign, but not including the equals sign?

The Id itself is composed of random letters and numbers each time it is generated, so using any part of that for reference isn't really an option.

Just add = symbol next to the negated character class. So that only = also got removed.

the_url.replace(/^[^=]+=/,"");

OR

> var s = "www.website.com?id=XXXXXXXXXXXX";
undefined
> s.match(/[^=]+$/)
[ 'XXXXXXXXXXXX',
  index: 19,
  input: 'www.website.com?id=XXXXXXXXXXXX' ]
> s.match(/[^=]+$/)[0]
'XXXXXXXXXXXX'

[^=]+ matches any character but not of = symbol one or more times. $ anchors refers the end of a line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM