javascript正则表达式允许带引号的多个单词 [英] javascript regex allow multiple words with quotes

查看:116
本文介绍了javascript正则表达式允许带引号的多个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个<textarea>,我需要验证双引号之间的所有内容是纬度还是经度.我进行了设置,以使单词不能使用超过3次,这很好.但是我有一条错误消息,显示何时使用了错误的格式.当用户集中注意力并且在引号之间未添加任何内容或不是经度或纬度时,我需要弹出错误消息.

I have a <textarea> where I need to validate that everything between double quotes is either latitude or longitude. I have it set up so that the word can not be used more than 3 times which is fine. But I have an error message that shows when the wrong format is used. I need the error to pop up when the user focuses out and hasn't put anything in between quotes or it's not latitude or longitude.

这是我的演示:

FIDDLE

FIDDLE

$('#test').on('keydown focusout', function(e){
var word  = 'latitude',
    count = this.value.match(new RegExp('"\\b'+word+'\\b"','g')) ||  [],
    limiter = $('#output');

$('#output').text(count.length);

return !(count.length > 2 && e.which != 8);

});

//Error - Max limit reached

$('#test').bind('keyup focusout', function(){  
limiter = $('#output');

if(limiter.text() == '3'){

    $('#limitReached').attr("class","hi");
    $('#limitReached').text("You cannot exeed more than 10 coorniates");
    $('#test').css({'border': '1px solid red'});

}
else{
     $('#limitReached').attr("class","bye");
     $('#limitReached').text("");
     $('#test').css({'border': '1px solid black'});
}
});

//Error - Format is wrong

$('#test').on('focusout', function(e){  
var word1  = 'latitude',
    word2  = 'longitude', 
    count = this.value.match(new RegExp('"\\b'+word1+'\\b"','g')) || [];

if ($(this).val() != count){

    $('#limitReached').attr("class","hi");
    $('#limitReached').html('Please use correct JSON format:<br> example - [{"latitude":33.851871,"longitude":-84.364336},]');
    $('#test').css({'border': '1px solid red'});

}
else{
     $('#limitReached').attr("class","bye");
     $('#limitReached').text("");
     $('#test').css({'border': '1px solid black'});
}
});

推荐答案

参考thg435的评论:

Referring to thg435's comment:

如果您尝试解析无效的json,则会抛出SyntaxError.请参见文档:

If you try to parse invalid json a SyntaxError is thrown. See the docs:

JSON.parse将字符串解析为JSON并返回解析后的值.

JSON.parse parses a string as JSON and returns the parsed value.

...

如果要解析的字符串不是有效的JSON,则会引发SyntaxError异常.

If the string to parse is not valid JSON, a SyntaxError exception is thrown.

示例代码:

try {
    var json = JSON.parse('[{"latitude":33.851871,"longitude":-84.364336}]');
    if (json.length > 3) throw new Error("Too many coordinates");
    _.each(json, function(coordinate) {
        if (!_.has(coordinate, 'latitude') || !_.has(coordinate, 'longitude')) throw new Error("Invalid coordinate pair found");
    });
}
catch (e) {
    // handle your invalid json and return to stop further execution
    console.error(e);
    return;
}

console.info('ok');

此方法使用下划线

在此处查看工作副本: http://jsfiddle.net/fcvyL/2/

See for a working copy here: http://jsfiddle.net/fcvyL/2/

这篇关于javascript正则表达式允许带引号的多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆