2016-06-09 129 views

回答

1

你可以使用積極的lookahead正則表達式。

console.log('Hello <!Doctype'.match(/.*(?=<!Doctype)/));

0

使用替換法 例 返回,其中 「微軟」 替換爲 「W3Schools的」 字符串:

var str = "Hello <!Doctype"; 
var res = str.replace("<!Doctype", ""); 

資源的結果將是:

你好

+0

我說從<!Doctype ...開始,這意味着該字符串可能是「Hello <!Doctype html」也..所以請提供一個通用公式.. – Abhishek

0

下面是使用String.indexOf函數的簡單的解決方案:

function removeDocTypeStr(str) { 
    return str.substring(0, str.indexOf("<!Doctype")); 
} 

console.log(removeDocTypeStr("Hello <!Doctype html")); // "Hello " 
console.log(removeDocTypeStr("Hi <!Doctype"));   // "Hi " 
0
var str="Hello ABC <!Doctype ABCDEFG"; 
var new_str = str.split("<!Doctype")[0]; 
console.log(new_str); 

只需使用分割功能。