JavaScript Regular Expression Syntax
I am trying to create a regular expression that checks for letters, numbers, and underscores. In .NET, I can do "^\w+$". Ho开发者_如何学JAVAwever, I am not that familiar with the JavaScript syntax. Can somebody help me out?
Thank you!
One obvious difference is that in JavaScript, you write the regex as /pattern/flags
-- this is Perl-style. Your "example" would then be ^\w+$
→ /^\w+$/
.
For example, replace multiple e
's with one e
, case-insensitive search (hence the i
flag):
var s='qweEEerty';
s=s.replace(/e+/i, 'e');
Returns: qwerty
.
That same expression will work in JavaScript (there are some differences between .NET regular expressions and JavaScript regular expressions but not in this example).
I recommend that you read Using Regular Expressions with JavaScript and ActionScript to learn a bit more about JavaScript's regular expression implementation.
精彩评论