末(文本)如何搜索詞並開始使用它的字符串,並尋找另一個詞結束的字符串,例如:如何(從文)開始,字符串
var string = "Hello whole world";
先從「你好」,並與「世界」結束
所以這將是:
var string = " whole ";
希望我的問題是清楚的
末(文本)如何搜索詞並開始使用它的字符串,並尋找另一個詞結束的字符串,例如:如何(從文)開始,字符串
var string = "Hello whole world";
先從「你好」,並與「世界」結束
所以這將是:
var string = " whole ";
希望我的問題是清楚的
function findBetween(text, start, end) {
var startIndex = text.indexOf(start);
var endIndex = text.lastIndexOf(end);
if (startIndex === -1) {
throw new Error("Start string not found.");
} else if (endIndex === -1) {
throw new Error("End string not found.");
}
startIndex += start.length;
if (startIndex > endIndex) {
throw new Error("Start found after end!");
}
return text.substring(startIndex, endIndex);
}
console.log(findBetween("Hello whole world", "Hello", "world"));
// Output:
// whole
謝謝你解決。有用! – anmml
var text = "Hello whole world";
var re = /^\w+\s+(.*)\s+\w+$/;
var result = re.exec(text);
alert(result[1]);
它的工作,但我如何定製開始和結束的話? – anmml
@anmml這不是原始問題的一部分。你可以做'var start =「Hello」; var end =「world」; var re = new RegExp(「^」+ start +「\ s +(。*)\ s +」+ end +「$」);' – Tibrogargan
我試過了,但它不起作用 – anmml
你在哪裏卡住,當你試着自己寫這個腳本? –
有沒有答案.. – anmml