regex - How do I match across line breaks with JavaScript regular expressions? - Stack Overflow

I have this expression:$(document).ready(function(){$.validator.addMethod("regex",function(va

I have this expression:

$(document).ready(function(){
    $.validator.addMethod(
        "regex",
        function(value, element) {
            return this.optional(element) || /^(?!.*www)(?!.*http)(?!.*@)(?!.*\)(?!.*\.pt)(?!.*co\.uk).+$/i.test(value);
        },
        "Description field can not include e-mail and/or urls."
    );
    $("#regForm").validate();
});

If I have a text like this, I get an error, because of the line break

A Loja virtual possui uma vasta linha de bijouterias folheadas a ouro e prata. São produtos bastante procurados e  preços muito acessíveis(50% de desconto no atacado).
Ha mais de 10 anos no mercado de folheados.

Produtos de excelente qualidade.

If I have this text, without line breaks, it works fine:

A Loja virtual possui uma vasta linha de bijouterias folheadas a ouro e prata. São produtos bastante procurados e  preços muito acessíveis(50% de desconto no atacado).Ha  mais de 10 anos no mercado de folheados.Produtos de excelente qualidade.

How can I fix this?

I have this expression:

$(document).ready(function(){
    $.validator.addMethod(
        "regex",
        function(value, element) {
            return this.optional(element) || /^(?!.*www)(?!.*http)(?!.*@)(?!.*\.)(?!.*\.pt)(?!.*co\.uk).+$/i.test(value);
        },
        "Description field can not include e-mail and/or urls."
    );
    $("#regForm").validate();
});

If I have a text like this, I get an error, because of the line break

A Loja virtual possui uma vasta linha de bijouterias folheadas a ouro e prata. São produtos bastante procurados e  preços muito acessíveis(50% de desconto no atacado).
Ha mais de 10 anos no mercado de folheados.

Produtos de excelente qualidade.

If I have this text, without line breaks, it works fine:

A Loja virtual possui uma vasta linha de bijouterias folheadas a ouro e prata. São produtos bastante procurados e  preços muito acessíveis(50% de desconto no atacado).Ha  mais de 10 anos no mercado de folheados.Produtos de excelente qualidade.

How can I fix this?

Share Improve this question edited Nov 23, 2012 at 2:01 Andy Lester 93.9k16 gold badges105 silver badges160 bronze badges asked Nov 22, 2012 at 21:05 RANRAN 594 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

. does not match linebreaks by default. Usually, the s (called singleline or dotall) modifier changes that. Unfortunately, it is not supported by JavaScript.

There is (a slightly verbose) trick to get around that. The character class [\s\S] matches any space and any non-space character. I.e. any character. So you would need to go with this:

/^(?![\s\S]*www)(?![\s\S]*http)(?![\s\S]*@)(?![\s\S]*\.)(?![\s\S]*\.pt)(?![\s\S]*co\.uk)[\s\S]+$/i

Alternatively, (only in JavaScript) you can use the "candle operator" [^] which also matches any single character (since it's a negation of the empty character class, i.e. it matches any character not in the empty set). Whether you find that more or less readable is a matter of taste I guess:

/^(?![^]*www)(?![^]*http)(?![^]*@)(?![^]*\.)(?![^]*\.pt)(?![^]*co\.uk)[^]+$/i

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744250354a4565138.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信