1

For example I have a bunch of string of this format "/something/@wherever" "/some/#whatiwant" I'm only interested in what comes after #'s in this case.

For the two suggested duplicates:

Regex: find whatever comes after one thing before another thing

The accepted answer is no where near as succinct as the one I accepted and honestly would have left me confused had I been able to think up a way to search for "regex find whatever comes after one thing before another thing"

Reference - What does this regex mean?

Is a reference manual for regex and not an answer to my question, if it were an answer to my question ALL regex questions should just be sent that direction, and are therefore duplicates.

Community
  • 1
  • 1
cheshirecatalyst
  • 370
  • 1
  • 4
  • 14

4 Answers4

2

In this example, capturing the non white space(for \S+) after the # in group-1.

var testString = "/some/#whatiwant";
var testOutput = testString.match(/#(\S+)/)[1];
// alert(testOutput);

If you want to use both # and @ for parsing, then use character class []

var testOutput = testString.match(/[#@](\S+)/)[1];
Sabuj Hassan
  • 35,286
  • 11
  • 68
  • 78
1

It depends on which flavor of regex you're using, but what you want is positive lookbehind:

(?<=#).*

This regex will match everything AFTER the hashtag, but not the hashtag itself.

EDIT: I didn't look at the javascript tag. js doesn't natively support lookbehind, so you'll have to use a capture group, like so:

var str = '/something/some#whatIwant',
reg = /#(.*)/;
alert(str.match(reg)[1]);
James M. Lay
  • 1,606
  • 18
  • 26
0

The regex would be [^#]+#(.+).

mzedeler
  • 3,613
  • 3
  • 20
  • 34
0

Try:

var arr = ["/something/@wherever","/some/#whatiwant"];
var reg = /[#@](\w+)$/; // matchs after # and @ signs

for ( var i = 0; i < arr.length; i++ ) {
    console.log(reg.exec(arr[i])[1]);
}

RegExp match after # and @ (if you don't want @ to be matched onward just remove it from RegExp)

JSFIDDLE

nanobash
  • 5,063
  • 6
  • 33
  • 56