簡體   English   中英

如何在javascript中的html字符串中找到包含標簽的html子字符串?

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

我有一個字符串和子字符串,我想獲得該字符串string.indexOf字符串的第一個索引,為此我使用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 = "<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,這是因為它搜索匹配'sub_str'的字符串。 在給定的字符串中找不到。 所以它返回-1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM