Javascript regular expression to match various strings
I have a question that I can't resolve. I have strings that contains hashtags, for example '开发者_开发技巧#question' or '#idea'.
The question is how extract the hashtag?
var hashtag = str.match(/#question?#idea?/i);
Is this regexp correct?
Your line
var hashtag = str.match(/#question?#idea?/i);
checks whether a string contains a literal #questio
, optionally followed by n
, followed by #ide
, optionally followed by a
. If that's the case, hashtag[0]
will contain the matched text. This is obviously not what you want.
Try this:
// match a #, followed by either "question" or "idea"
var myregexp = /#(question|idea)\b/i;
var match = myregexp.exec(subject);
if (match != null) {
result = match[1]; // will contain "question" or "idea"
} else {
result = "";
}
The \b
at the end of the regex is a word boundary anchor. It ensures that only #question
but not #questionnaire
will be matched.
If you don't know the names of the hashtags beforehand, use
var myregexp = /#(\w+)/;
(this allows ASCII letters, digits and underscore as possible hashtag characters - I don't know if those are the only valid characters. If not, you can use a character class. For example, to allow dashes, too:
var myregexp = /#([\w-]+)/;
No your regex is not correct.
/#question?#idea?/i
You search for each character in your regex, in this order. The ?
makes the character before comes 0 or 1 times, the i at the end makes the expression ignoreCase, i.e. treat upper and lower case the same. You will find:
#question#idea
#questio#ide
#questio#idea
#QUEStion#IDea
but not
#question
#idea
Do you have a fixed set of tags, can they contain only letters, or also numbers, or other characters?
You can check your regexes e.g. here online My example
var str = "/foo=bar?#quesAion#dwdad";
var regi = new RegExp("(#[a-z0-9_-]*)", "gi");
var hashtag = str.match(regi);
Hashtag will be an array of found strings or null if not found any.
精彩评论