开发者

JavaScript Regular Expression Syntax

开发者 https://www.devze.com 2023-04-02 10:29 出处:网络
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.

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.

0

精彩评论

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