2012-04-25 96 views
0

我有一個字符串和子字符串,我想獲得該字符串中的子字符串的第一個索引,我使用string.indexOf爲此目的,但是當有html標籤時,它始終返回-1。我正在CKEDITOR工作如何在javascript中查找包含html字符串標籤的html子字符串?

請提出任何方法。


(從評論)

我談論這件事情現在​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

+4

請發送示例代碼。 – Blender 2012-04-25 06:20:11

+0

你能提供你的代碼嗎? – 2012-04-25 06:20:13

+0

startpoint = all_data.indexOf(selected_text); – 2012-04-25 06:23:27

回答

0
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/ 使用sub_str沒有引號的indexOf() ,如果我們使用引號,它將返回-1,爲什麼,因爲它搜索的字符串'sub_str'。它在給定的字符串中找不到。所以它返回-1

+0

其實我在ckeditor中選擇html,當我嘗試找到所選html的索引時,它會給我錯誤的位置,當有多個html標籤時 – 2012-04-25 06:48:18

+0

@AbubakarShams以前你沒有在你的問題中提到ckeditor,我不知道沒有關於ckeditor的知識。我希望上面的代碼有效。 – Valli69 2012-04-25 07:00:04

相關問題