1

With regex, how can I match everything after and including the last occurence of an empty space/tab?

For example, 1283128318231283128213 123881213 81328 ajdh suu should match suu

Thanks, this is for JavaScript

Sam
  • 201
  • 3
  • 11

3 Answers3

1

Try this out:

\s[^\s]+$

Follow link below to see it in action with the test data you provided in your question.

Regular expression visualization

Debuggex Demo

nickytonline
  • 6,855
  • 6
  • 42
  • 76
0

Use \s to match a tab or a white-space character. Since your input won't contain any newline character, you may free to use the below regex.

> "1283128318231283128213 123881213 81328 ajdh suu".match(/\s\S*$/)[0]
' suu'

Update:

> "PRICE $153.00 $133.05 ".match(/\s\S*/)[0].replace(/\D/g, "")
'15300'
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • That seems to work for the string I originally posted, but for some reason it isn't working in this context (using jQuery) `var url = 'https://steamcommunity.com/market/listings/730/%E2%98%85%20Flip%20Knife%20%7C%20Doppler%20%28Factory%20New%29/render?start=0&count=1&currency=1&language=english&format=json'; $.get(url+'?_'+$.now()).done(function(f) { console.log($(f.results_html).find('.market_listing_their_price').text().replace(/\n/g, "").replace(/\s\S*$/g, "$REP")) });` – Sam May 10 '15 at 03:06
  • I didn't find any space in your input. – Avinash Raj May 10 '15 at 03:07
  • replace(/\t/g, "") makes the output `PRICE$153.00$133.05` versus `PRICE $153.00 $133.05 ` – Sam May 10 '15 at 03:12
  • then how come you get the last part if you replace the tab with empty string? You may try `[ \t]\S*$` after replacing the newline charcater. – Avinash Raj May 10 '15 at 03:15
  • I just want the code I posted in this comment to return the first price (15300), how can I do that?? I was planning on stripping the second price then stripping nonalphanumeric characters – Sam May 10 '15 at 03:17
0

Try this:

var re = /^(.*)( |\t)(.*)$/;
var str = "foo bar";
var newstr= str.replace(re, "$3");
console.log(newstr);

var strTwo= "foo    bar";
var newstrTwo = strTwo.replace(re, "$3");
console.log(newstrTwo);
hatef
  • 5,491
  • 30
  • 43
  • 46