开发者

Regex flavors translation

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

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

0

精彩评论

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