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/

1
  • What exactly is your question? Which regex doesn't work? Commented Mar 2, 2012 at 22:11

4 Answers 4

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());
Sign up to request clarification or add additional context in comments.

4 Comments

+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.
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).
(\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…
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.
1

Non-# character followed by anything:

[^#].* 

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

Comments

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.

Comments

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 "#"?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.