首页 文章

引号字符串列表的正则表达式匹配 - 未引用

提问于
浏览
10

在Javascript中,以下内容:

var test = '"the quick" "brown fox" "jumps over" "the lazy dog"';
var result = test.match(/".*?"/g);
alert(result);

产生“快速”,“棕色狐狸”,“跳过”,“懒狗”

我希望每个匹配的元素都不被引用:快速的棕色狐狸跳过,懒惰的狗

什么正则表达式会这样做?

7 回答

  • 0

    这似乎有效:

    var test = '"the quick" "brown fox" "jumps over" "the lazy dog"';
    var result = test.match(/[^"]+(?=(" ")|"$)/g);
    alert(result);
    

    注意:这与空元素(即“”)不匹配 . 此外,它不适用于不支持JavaScript 1.5的浏览器(前瞻是1.5功能) .

    有关详细信息,请参阅http://www.javascriptkit.com/javatutors/redev2.shtml .

  • 1

    它不是一个正则表达式,而是两个更简单的正则表达式 .

    var test = '"the quick" "brown fox" "jumps over" "the lazy dog"';
    
    var result = test.match(/".*?"/g);
    // ["the quick","brown fox","jumps over","the lazy dog"]
    
    result.map(function(el) { return el.replace(/^"|"$/g, ""); });
    // [the quick,brown fox,jumps over,the lazy dog]
    
  • -1

    grapefrukt的回答也有效 . 我会使用David的变体

    match(/[^"]+(?=("\s*")|"$)/g)
    

    因为它正确处理任意数量的空格和字符串之间的标签,这是我需要的 .

  • 7

    您可以使用Javascript replace() method去除它们 .

    var test = '"the quick" "brown fox" "jumps over" "the lazy dog"';
    
    var result = test.replace(/"/, '');
    

    还有更多的东西,而不仅仅是摆脱双引号?

  • 0

    这就是我在actionscript3中使用的内容:

    var test:String = '"the quick" "brown fox" "jumps over" "the lazy dog"';
    var result:Array = test.match(/(?<=^"| ").*?(?=" |"$)/g);
    for each(var str:String in result){
        trace(str);
    }
    
  • 4

    用于匹配简单引号和双引号之间的内容,以处理转义的引号 .

    由于搜索引擎首先驱使我在这里,我真的想让那些希望检查引号对的人们找到更通用的问题:https://stackoverflow.com/a/41867753/2012407 .

    正则表达式将获得良好形式的引号对之间的完整内容,例如 '"What\'s up?"' ,例如 // Comment./* Comment. */ 之类的代码注释中没有 .

  • 0

    这是一种方式:

    var test = '"the quick" "brown fox" "jumps over" "the lazy dog"';
    var result = test.replace(/"(.*?)"/g, "$1");
    alert(result);
    

相关问题