gpt4 book ai didi

javascript - 匹配以下数组中的双引号

转载 作者:行者123 更新时间:2023-11-30 17:01:15 25 4
gpt4 key购买 nike

这是数组的数组:

[ 'markdown',
[ 'para', 'This is a ', [ 'em', 'test' ] ],
[ 'hr' ],
[ 'para', {class: 'noind'}, 'another test' ],
[ 'para', '"test with double quotes"' ],

我知道如何匹配para:

for (i = 1; i < jsonml.length; i++) {
if (jsonml[i][0] === 'para') {
// do stuff
}
}

现在我只想匹配带有双引号的 para ([ para, '"test with double quotes"' ])。

我试过这个:

if (jsonml[i][0] === 'para' && jsonml[i][1].match(/"/g)) {

但我得到 TypeError: Cannot call method 'match' of undefined。可能是因为 [hr][ 'para', {class: 'noind'}, 'another test' ]?如果是这样,我怎样才能使代码工作?

最佳答案

JSBIN DEMO

在嵌套数组中查找带双引号的字符串的简单实用方法:

   var array = [ 'ma"r"kdown', [ 'para', 'This is a ', ['em', 'test']],
[ 'hr' ],
[ 'para', {class: 'noind'}, 'another test' ],
[ 'para', '"test with double quotes"' ]];

function checkDoubleQuotes(array) {
array.forEach(function(value) {
if({}.toString.call(value)==="[object Array]") {
return checkDoubleQuotes(value);
}
if(typeof value==='string' && value.match(/"/g)) {
console.log("matched : "+value);
}

})
}

checkDoubleQuotes(array);

关于javascript - 匹配以下数组中的双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28815013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com