0

I'm searching a regex in js to match string doesn't start with # the opposite of this function

   String.prototype.parseHashtag = function() {
        return this.match(/[#]+[A-Za-z0-9-_]+/g);
    }

    t="#lorem #ipsum yes no";
    console.log(t.parseHashtag()); // ["#lorem", "#ipsum"] 

I found this Regex: Finding strings that doesn't start with X

but the regex doesn't work /([^#]|^)[a-z]/ or maybe I'm tired… I do a replace() but really be curious to understand how to do it in match()!

Here is a js : http://jsfiddle.net/d8HVU/1/

Community
  • 1
  • 1
benoît
  • 1,473
  • 3
  • 13
  • 31

4 Answers4

1

Non-# character followed by anything:

[^#].* 

See it working at http://refiddle.co/1rj

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Paul Alexander
  • 31,970
  • 14
  • 96
  • 151
1

I tried this and is working well for Javascript on your sample input. I hope it is also ok for other inputs. Test it well. The other answers did not work out in Javascript.

String.prototype.parseHashtag = function() {
    return this.match(/(\s|^)[^#][\w\d-_]+\b/g);
}

t="#lorem #ipsum yes no";
alert(t.parseHashtag());
jdehaan
  • 19,700
  • 6
  • 57
  • 97
  • +1 probably you are right and this matches the best what OP wants. 2 small things, it will match the whitespace before the word (I think no chance to avoid this, cause Javascript does not support lookbehinds), second no need for `\d` in the charclass, its included in `\w`. – stema Mar 02 '12 at 22:36
  • Oh, and an important point: escape the dash in the character class or move it to the end it defines a character range (maybe not needed because before is a predefined class, the `\d`). – stema Mar 02 '12 at 22:39
  • (\s|^) I need to learn regex… ^ is for start or 'not' when it's in [] ? but don't know when is single with | (it's OR ?)… regex is the new sudoku for webdev… – benoît Mar 02 '12 at 23:19
  • Yes, all right. | is or. Like stema said, maybe you also have to escape the dash, I'm not an expert for JS regexes. I tried to keep as much as possible from your original regex. – jdehaan Mar 02 '12 at 23:29
0

If I was trying to match the last hash tag and all chars that follow it, I'd use this regex:

/#[^#]*$/

Working demo: http://jsfiddle.net/jfriend00/nE2bM/

If you're trying to do something different, please clarify exactly what you're asking for.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

You can use a negative lookahead for this

^(?!#).*

See it here on Regexr

This matches each string that does not start with a "#".

OK, I am not sure, do you want to match the start of the string or kind of a "word" that does not start with "#"?

stema
  • 90,351
  • 20
  • 107
  • 135