gpt4 book ai didi

javascript - 检测字符串是否包含表格

转载 作者:太空狗 更新时间:2023-10-29 15:24:17 26 4
gpt4 key购买 nike

我有一个关于检查 string 的问题。

string 来自 ckeditor,因此用户可以输入任何内容。

variable 名称是 htmlData 如下所示:

test here<br />
<table border="1" cellpadding="1" cellspacing="1" style="width: 500px;">
<tbody>
<tr>
<td>
111</td>
<td>
222</td>
</tr>
<tr>
<td>
333</td>
<td>
444</td>
</tr>
<tr>
<td>
555</td>
<td>
666</td>
</tr>
</tbody>
</table>
<br />
second test

我想检测用户是否添加了一个table结构,我已经尝试过了

 if(htmlData.indexOf('</table>').length > -1){
console.log('table detected')
}

但它在我的控制台中没有显示任何内容。任何人都可以对此给出提示吗?

非常感谢!

最佳答案

String.<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf" rel="noreferrer noopener nofollow">indexOf()</a>返回一个原始数值,具体来说:

the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex or -1 if the value is not found.

这些基元没有属性,即:length .

if(htmlData.indexOf('</table>')<b>.length</b> > -1){
console.log('table detected')
}

因此,只需删除 .length从你的代码:

if(htmlData.indexOf('</table>') > -1){
console.log('table detected')
}

关于javascript - 检测字符串是否包含表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17929745/

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