Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果索引未知,如何在javascript中查找子字符串?_Javascript_Substr - Fatal编程技术网

如果索引未知,如何在javascript中查找子字符串?

如果索引未知,如何在javascript中查找子字符串?,javascript,substr,Javascript,Substr,假设我有一个包含HTML文件的字符串。如何仅获取脚本部分作为子字符串 示例字符串为: str="<html>...<script>...</script>...</html>" str=“……” 我只需要脚本部分 <script type = "text/javascript"> function Showques() { var xmlhttp; if (window.XMLHttpRequest) {

假设我有一个包含HTML文件的字符串。如何仅获取脚本部分作为子字符串

示例字符串为:

str="<html>...<script>...</script>...</html>"
str=“……”
我只需要脚本部分

<script type = "text/javascript">
function Showques()
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    } 
    else 
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    //getting response from servlet
    xmlhttp.onreadystatechange=function();
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) 
        {
            var localString=xmlhttp.responseText;
            var locationstart =localString.indexOf('<script>');
            var locationend = localString.indexOf('</script>');
            //eval(xmlhttp.responseText);
            document.getElementById("ques").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST","XmlApp",true);
    xmlhttp.send();
}
</script>

函数Showques()
{
var-xmlhttp;
if(window.XMLHttpRequest)
{
xmlhttp=新的XMLHttpRequest();
} 
其他的
{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
//从servlet获取响应
xmlhttp.onreadystatechange=function();
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
var localString=xmlhttp.responseText;
var locationstart=localString.indexOf(“”);
var locationend=localString.indexOf(“”);
//eval(xmlhttp.responseText);
document.getElementById(“ques”).innerHTML=xmlhttp.responseText;
}
}
open(“POST”,“XmlApp”,true);
xmlhttp.send();
}
当我在
indexOf
方法中给出
时,它被认为是我脚本的结尾,我在开始时声明了这个脚本,其余的事情我都会出错。

使用

string.indexOf(searchstring, start)
在字符串中查找正确的索引。然后可以在substr函数中使用索引

希望这有帮助。

试试这个,然后

好的,根据你的例子做这个

 String str = "<html>...<script>...</script>...</html>"
 int locationStart = str.indexOf('<script>');
 int locationend = str.indexOf('</script>');
 strinf finalString =string.substring(locationStart , locationend );   //ur final string value
String str=“……”
int locationStart=str.indexOf(“”);
int locationend=str.indexOf(“”);
strinf finalString=string.substring(locationStart,locationend)//ur最终字符串值

如果字符串是实际的html文件,则最好使用dom解析器。
如果您正在使用javascript,下面是一种简单的jQuery方法:

str="<html>...<script>...</script>...</html>";
$(str).find("script").get(0).outerHtml;
str=“……”;
$(str).find(“script”).get(0).outerHtml;

我的字符串如下所示:str=“……,我需要单独的脚本部分。我的字符串如下所示:str=“……,我需要单独的脚本部分。@user1101422:这是输出吗?
str="<html>...<script>...</script>...</html>";
$(str).find("script").get(0).outerHtml;