I am wondering if there is a good tool that translates RegEx from .NET(C#) syntax to Javascript, including all escaping rules. I have a RegEx that works in .NET inside RegExValidator but doesn't work inside Javascript:
^\s*\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s*$
Javascript开发者_如何学Go doesn't like apostrophy inside RegEx when used like sample below, but .NET didn't have a problem with that.
$('#<%= ContactEMail.ClientID %>').blur(function() {
if ((^\s*\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s*$)).test($(this).val())
{
$(this).next('span').show();
}
else
{
$(this).next('span').hide();
}
});
You are not using a JavaScript literal; use slashes to delimit one, instead of parentheses:
if (/^\s*\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s*$/.test($(this).val()))
Try this:
var regexp = /^\s*\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s*$/g;
if ($(this).val().match(regexp))
$(this).next('span').show();
else
$(this).next('span').hide();
Match info: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/match
Hope help
精彩评论