开发者

Can anyone explain me on how to form regular expression and explain this regular expression?

replace(/[^0-9]/g,''));
  1. Replace is a method
  2. What does / indicate?
  3. What d开发者_如何学Coes ^ indicate along with 0-9
  4. What does /g indicate?

Do we need to start a regular expression with / or can we start with anything?


The / introduces a regular expression literal (just like " and ' introduce string literals). A regular expression literal is in the form /expression/flags, where expression is the body of the expression, and flags are optional flags (i for case-insensitive, g for global, m for multi-line stuff).

The ^ as the first character within [] means any character not matching the following. So [^0-9] means "any character except 0 through 9".

The /g ends the regular expression literal and includes the "global" flag on it. Without the g, replace would only replace the first match, not all of them.

In all, what that does is replace any character that isn't 0 through 9 with a blank — e.g., removes non-digits. It could be written more simply as:

var result = str.replace(/\D/g, '');

...because \D (note that's an upper-case D) means "non-digit".

MDC has a decent page on regular expressions.


The / and / are the start and end of the regex pattern, the g mean global (anything after the 2nd / is an optional modifier for the regex).

^ means not.

So in this case it'll remove any character that isn't a number.


  1. See the manual for replace
  2. See regular expression literals
  3. See using special characters
  4. See searching with flags


  1. replace is method of string type
  2. / / indicates there's a regular expression inside of them
  3. ^ inside [] means "not"
  4. "g" means to replace globally

regular expressions in javascript should put in to a pair of "/"


This W3 Schools tutorial should cover most of the basics. This other tutorial covers the flasg, such as /g which can be passed to the regex engine.


  1. yes
  2. start and end of regex
  3. not, that just basically means, match any non-integer
  4. global replacement, the effect of not having that is replacement only done for the first encounter.

At least in javascript, yes you have to use /.


/ indicates the beginning and end of the regexp. Hence in your case [^0-9] is the regex. ^ indicates the start of line /g indicates the substitution to take place for all the match - globl, and not only for the first match.


/g enables "global" matching. When using the replace() method, specify this modifier to replace all matches, rather than only the first one.

/ start regex

^ match except the symbols 0-9


Well, as to creating one, this forum is not the best for that -- it is a rather large question, best left to one of the best resources on RegExp that I know of.

It looks like you're in JS, so:

  • replace is a method of String. It replaces the provided expression with the second string, in this case nothing.
  • In JavaScript / must begin and end all RegEx's, all / in the middle must be escaped with a \ (so they look like this: \/). In other languages (PHP, Perl being some of the most prominent), you can use other characters such as ~ and #.
  • ^ inside of [] means negation, - means range, so [^0-9] means "not 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9" [0-9] does have a shorthand of \d. So /[^\d]/g is a valid, alternate way to say the same thing.
  • /g means "global" as in "match all incidents, not just the first.

Your expression means, "replace all non-digits with nothing".


The / encapsulate your pattern (you need to escape / with \ if you want to use it in pattern) and the trailing character after the slashes are modifiers. 'g' in this case means global search (i.e. find all matches)

^ is negation.. [0-9] is range indicating all numbers from 0 to 9. so [^0-9] means anything except numbers So This regex basically replaces anything except numbers in the string with '' (i.e. remove them)

Regex has lots of other features, you should research them!


What it does: Removes all non-numeric (0-9) characters.

  • The forward slash (/) is used when you declare RegExp literals
  • The [^0-9] means any character OTHER THAN 0-9. The ^ means "other than". You can remove it and it'll look for only a character 0-9.
  • The /g represents global replacement.

So this will look for any non-number character and replace it with nothing. As Shamim notes, regular-expressions.info/is a great site. Best of luck!

You can try out javascript regex's on this site: http://regexpal.com/ Couples with http://www.regular-expressions.info/tutorial, it's a great resource for learning.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜