用双引号引起来的多行字符串的Javascript正则表达式,可能包含引号

问题描述

|| 我想编写一个JavaScript正则表达式,以匹配引号中可能包含引号的多行字符串。最终报价将以逗号终止。 例如:
\"some text between the quotes including \" characters\",
此字符串以
\"
开始,以
\",
结尾,并包含
\"
个字符。 我该如何工作? 我想真正的问题是我如何匹配以
\"
开始并以
\",
结尾的多行字符串?     

解决方法

        匹配许多非
\"
\"
不跟
,
的匹配项:
/\"((?:[^\"]|\"(?!,))*)\",/
或使用惰性量词:
/\"([\\0-\\uffff]*?)\",/
    ,        一个简单的match()不起作用吗?您还需要使用\\ s \\ S技巧使点包含换行符(实际上,这使它接受了以前的每个字符):
var str = \"bla bla \\\"some text between the quotes \\n including \\\" characters\\\",bla bla\";
var result = str.match(/\"([\\s\\S]+)\",/);
if (result == null) {
 // no result was found
} else {
 result = result[1];
 // some text between the quotes
 // including \" characters
}
    ,        使用正则表达式确实很棘手,我会尝试这样的事情:
var getQuotation = function(s) {
  var i0 = s.indexOf(\'\"\'),i1 = s.indexOf(\'\",\');
  return (i0 && i1) ? s.slice(i0+1,i1) : undefined;
};

var s = \"He said,\\\"this is a test of\\n\" +
        \"the \\\"emergency broadcast\\\" system\\\",obviously.\";
getQuotation(s); // => \'this is a test of
                 //     the \"emergency broadcast\" system\'
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...