我试图从一个更大的字符串中提取一个字符串,它得到了a:和a之间的所有东西;
当前的
Str = 'MyLongString:StringIWant;'
期望输出值
newStr = 'StringIWant'
我试图从一个更大的字符串中提取一个字符串,它得到了a:和a之间的所有东西;
当前的
Str = 'MyLongString:StringIWant;'
期望输出值
newStr = 'StringIWant'
当前回答
下面的函数获取第一个匹配项
function getStringBetween(x: string, start: string, end: string) {
const regex = new RegExp(`${start}(.*?)${end}`)
if (regex.test(x)) {
return regex.exec(x)![1]
} else return undefined
}
有测试
test("getStringBetween", () => {
const result = getStringBetween("<em> Jai Ram</em>", "<em>", "</em>")
expect(result).toEqual(" Jai Ram")
const result1 = getStringBetween(
"hare Jai Ram hare hare hare",
"hare",
"hare"
)
expect(result1).toEqual(" Jai Ram ")
})
其他回答
@Babasaheb Gosavi回答是完美的,如果你有一个子字符串出现(“:”和“;”)。但一旦出现多次,就有点棘手了。
我在多个项目中提出的最佳解决方案是在一个对象中使用四个方法。
第一种方法:实际上是从两个字符串之间获得一个子字符串(但是它只会找到一个结果)。 第二种方法:删除(可能的)最近发现的结果及其前后的子字符串。 第三种方法:将上述两种方法递归地处理一个字符串。 第四种方法:将应用第三种方法并返回结果。
Code
说得够多了,让我们看看代码:
var getFromBetween = {
results:[],
string:"",
getFromBetween:function (sub1,sub2) {
if(this.string.indexOf(sub1) < 0 || this.string.indexOf(sub2) < 0) return false;
var SP = this.string.indexOf(sub1)+sub1.length;
var string1 = this.string.substr(0,SP);
var string2 = this.string.substr(SP);
var TP = string1.length + string2.indexOf(sub2);
return this.string.substring(SP,TP);
},
removeFromBetween:function (sub1,sub2) {
if(this.string.indexOf(sub1) < 0 || this.string.indexOf(sub2) < 0) return false;
var removal = sub1+this.getFromBetween(sub1,sub2)+sub2;
this.string = this.string.replace(removal,"");
},
getAllResults:function (sub1,sub2) {
// first check to see if we do have both substrings
if(this.string.indexOf(sub1) < 0 || this.string.indexOf(sub2) < 0) return;
// find one result
var result = this.getFromBetween(sub1,sub2);
// push it to the results array
this.results.push(result);
// remove the most recently found one from the string
this.removeFromBetween(sub1,sub2);
// if there's more substrings
if(this.string.indexOf(sub1) > -1 && this.string.indexOf(sub2) > -1) {
this.getAllResults(sub1,sub2);
}
else return;
},
get:function (string,sub1,sub2) {
this.results = [];
this.string = string;
this.getAllResults(sub1,sub2);
return this.results;
}
};
如何使用?
例子:
var str = 'this is the haystack {{{0}}} {{{1}}} {{{2}}} {{{3}}} {{{4}}} some text {{{5}}} end of haystack';
var result = getFromBetween.get(str,"{{{","}}}");
console.log(result);
// returns: [0,1,2,3,4,5]
获取两个子字符串之间的字符串(包含多于1个字符)
function substrInBetween(whole_str, str1, str2){
if (whole_str.indexOf(str1) === -1 || whole_str.indexOf(str2) === -1) {
return undefined; // or ""
}
var strlength1 = str1.length;
return whole_str.substring(
whole_str.indexOf(str1) + strlength1,
whole_str.indexOf(str2)
);
}
注意,我使用indexOf()而不是lastIndexOf(),因此它将检查这些字符串的第一次出现
下面是一个可重用的函数,它允许你使返回的子字符串包含或排除,然后可选地修剪它:
function get_substring(full_string, substring_1, substring_2, inclusive, trim)
{
if (full_string === null) { return null; };
let substring_1_start = full_string.indexOf(substring_1);
if (substring_1_start === -1 ) { return null; }
let substring_2_start = full_string.indexOf(substring_2, substring_1_start);
if (substring_2_start === -1 ) { return null; }
let substring_1_end = substring_1_start + substring_1.length;
let substring_2_end = substring_2_start + substring_2.length;
let return_string = inclusive ? (full_string.substring(substring_1_start, substring_2_end)) : (full_string.substring(substring_1_end, substring_2_start));
return trim ? return_string.trim() : return_string;
}
使用例子:
//Returns 'cake and ice cream'
get_substring('I like cake and ice cream', 'cake', 'cream', true, true);
//Returns ' and ice '
get_substring('I like cake and ice cream', 'cake', 'cream', false, false);
//Returns 'and ice'
get_substring('I like cake and ice cream', 'cake', 'cream', false, true);
//Returns null
get_substring('I like cake and ice cream', 'cake', 'cookies', false, false);
//Returns null
get_substring('I like cake and ice cream', null, 'cream', false, false);
你可以试试这个
var mySubString = str.substring(
str.indexOf(":") + 1,
str.lastIndexOf(";")
);
您可以使用这个函数-
function getStringInBetween(string, start , end) {
// start and end will be excluded
var indexOfStart = string.indexOf(start)
indexOfStart = indexOfStart + start.length;
var newString = string.slice(indexOfStart)
var indexOfEnd = newString.indexOf(end)
return newString.slice(0, indexOfEnd)
}
对于前-
let string = "<div class = 'mice'> I know how to code </div>"
let start = "<div class = 'mice'> "
let end = " </div>"
//then, getStringInBetween(string, start, end) returns "I know how to code"