如何在 javascript 中查找 html 子字符串,包括 html 字符串中的标记

How to find html substring including tags in a html string in javascript?

本文关键字:字符串 html 包括 查找 javascript      更新时间:2023-09-26

我有一个字符串和子字符串,我想获取该字符串中子字符串的第一个索引,我为此目的使用string.indexOf但是当有html标签时,它总是返回-1。我在 CKEDITOR 工作

请提出任何方法。


(来自评论)

我现在在谈论这件事​str = "<html><head><body><p>hello</p><p>world</p><body> </head></​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​html>"; sub_str = "<p>hello</p><p>world</p>"; str.indexOf(sub_str)将返回 -1

str = "<html><head><body><p>hello</p><p>world</p><body> </head></html>";
sub_str = "<p>hello</p><p>world</p>";
alert(str.indexOf(sub_str));

请参阅此处的代码 http://jsfiddle.net/wQSbe/7/在 indexof() 中使用不带引号的sub_str,如果我们与引号一起使用,它将返回 -1,为什么因为它搜索一个 mache 'sub_str' 的字符串。它在给定的字符串中找不到。所以它返回 -1