0

I know I'm going to be bashed for this question. I know it is basics and I know it was asked many times, but it seems I'm to dumb to understand examples and I need solution. My problem:

user is sending a string to server. It should be /nick #nick_name I want to match only nick_name, I also want to consider situation, when user is not cooperating and send something like "/nick #nick_name and some trash". I don't need #, I don't need space after. I have a permanent brain fart on this.

I know about #(.*) but this match everything after a # (hash included). I need only one word. The perfect solution would be a lookbehind, to catch everything after #, but it's not working on JS. I really did my homework.

Zenek Wiaderko
  • 392
  • 2
  • 14
  • 1
    `input.match(/#(\S+)/)[1]` – Niet the Dark Absol Nov 10 '17 at 12:30
  • if you can ignore the spaces, then this can be done pretty easily by matching the first space character. – Parijat Purohit Nov 10 '17 at 12:30
  • Possible duplicate of [Reference - What does this regex mean?](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – revo Nov 10 '17 at 12:33
  • It's not a duplicate of the link you sent @revo, thanks anyway. The others was more helpful. It looks that there is no regex to show only part after #, without it? – Zenek Wiaderko Nov 10 '17 at 12:59
  • It's considered a possible duplicate due to the fact that you couldn't understand quantifiers in Regular Expressions. That reference will enlighten things up. – revo Nov 10 '17 at 13:07
  • Hej @revo, I don't pretend I know regex. It's rather complicated to me, but I read about my problem. And if you wrote: check this link, read about quantifiers, then I would do this as well and thanked you for this. But I ask a newbie question and I was sent to the page with multiple links and without any information other than: go learn. But I suspected I will be given responses like this :) It's a regex question after all :D. – Zenek Wiaderko Nov 10 '17 at 19:28

1 Answers1

0

try with this i think helpful

var str = "# /nik";
var reg = /#\s*/;
console.log(str);
if (reg.test(str)) {
  console.log(str.replace(reg, ""));
}
Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39
  • I know how to deal with input after regex, but I though there was some cleaner version. A regex to match just numbers and letters after hash, without hash etc. Otherwise I could just split string, take second value and get rid of #. Thanks anyway. #(\S+) will do the trick – Zenek Wiaderko Nov 10 '17 at 13:02