我试图从一个更大的字符串中提取一个字符串,它得到了a:和a之间的所有东西;
当前的
Str = 'MyLongString:StringIWant;'
期望输出值
newStr = 'StringIWant'
我试图从一个更大的字符串中提取一个字符串,它得到了a:和a之间的所有东西;
当前的
Str = 'MyLongString:StringIWant;'
期望输出值
newStr = 'StringIWant'
当前回答
如果您想从一个字符串中提取发生在两个分隔符(不同或相同)之间的所有子字符串,可以使用此函数。它返回一个包含所有子字符串的数组:
function get_substrings_between(str, startDelimiter, endDelimiter)
{
var contents = [];
var startDelimiterLength = startDelimiter.length;
var endDelimiterLength = endDelimiter.length;
var startFrom = contentStart = contentEnd = 0;
while(false !== (contentStart = strpos(str, startDelimiter, startFrom)))
{
contentStart += startDelimiterLength;
contentEnd = strpos(str, endDelimiter, contentStart);
if(false === contentEnd)
{
break;
}
contents.push( str.substr(contentStart, contentEnd - contentStart) );
startFrom = contentEnd + endDelimiterLength;
}
return contents;
}
// https://stackoverflow.com/a/3978237/1066234
function strpos(haystack, needle, offset)
{
var i = (haystack+'').indexOf(needle, (offset || 0));
return i === -1 ? false : i;
}
// Example usage
var string = "We want to extract all infos (essential ones) from within the brackets (this should be fun).";
var extracted = get_substrings_between(string, '(', ')');
console.log(extracted);
// output: (2) ["essential ones", "this should be fun"]
最初从PHP由raina77ow,移植到Javascript。
其他回答
下面是一个可重用的函数,它允许你使返回的子字符串包含或排除,然后可选地修剪它:
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);
@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]
我做的一个小函数,可以抓取之间的字符串,并且可以(可选地)跳过一些匹配的单词来抓取特定的索引。
同样,将start设置为false将使用字符串的开头,将end设置为false将使用字符串的结尾。
将pos1设置为您想要使用的开始文本的位置,1将使用start的第一次出现
Pos2和pos1做同样的事情,但是对于end,并且1将只在start之后使用end的第一次出现,在start之前出现的end将被忽略。
function getStringBetween(str, start=false, end=false, pos1=1, pos2=1){
var newPos1 = 0;
var newPos2 = str.length;
if(start){
var loops = pos1;
var i = 0;
while(loops > 0){
if(i > str.length){
break;
}else if(str[i] == start[0]){
var found = 0;
for(var p = 0; p < start.length; p++){
if(str[i+p] == start[p]){
found++;
}
}
if(found >= start.length){
newPos1 = i + start.length;
loops--;
}
}
i++;
}
}
if(end){
var loops = pos2;
var i = newPos1;
while(loops > 0){
if(i > str.length){
break;
}else if(str[i] == end[0]){
var found = 0;
for(var p = 0; p < end.length; p++){
if(str[i+p] == end[p]){
found++;
}
}
if(found >= end.length){
newPos2 = i;
loops--;
}
}
i++;
}
}
var result = '';
for(var i = newPos1; i < newPos2; i++){
result += str[i];
}
return result;
}
这是我刚做的东西。
注意,如果在启动后没有找到end,函数将返回启动后的所有内容。它还期望只有一次出现的开始和结束,如果有多次,它将只考虑第一次。
许可:公共领域
/**
* Extracts a string from `source` that is placed between `start` and `end`. The function
* considers only one instance of start and before, or the first instance and does not support
* multiple occurences otherwise. If end string is not found, it will return everything after
* `start` to the end of the string.
*/
export function stringBetween(source, start, end) {
if (source.indexOf(start) === -1) {
return null;
}
const sourceSplitByStartString = source.split(start);
// Note: If start string is the very first occurence in source string, the result will be an
// array where the first item is an empty string and the next item is of interest.
if (
sourceSplitByStartString.length === 1
|| sourceSplitByStartString[1] === ''
) {
// It means that start is either the entire string or is at the very end of the string, so there
// is not anything between
return '';
}
const afterStart = sourceSplitByStartString[1];
// If the after separator is not found, return everything after the start separator to the end
// of the string
if (afterStart.indexOf(end) === -1) {
return afterStart;
}
const afterStartSplitByEnd = afterStart.split(end);
if (afterStartSplitByEnd[0] === '') {
return '';
}
return afterStartSplitByEnd[0];
}
测试:
import { stringBetween } from './string';
describe('string utlities', () => {
describe('stringBetween', () => {
it('Extracts a substring between 2 other substrings', () => {
const sample1 = stringBetween('Black cat climbed the tree fast.', 'cat ', ' the tree');
expect(sample1).toBe('climbed');
const sample2 = stringBetween('Black cat climbed the tree fast.', 'Black ', ' fast.');
expect(sample2).toBe('cat climbed the tree');
});
it('extracts everything after start if end is not found', () => {
const sample2 = stringBetween('Black cat climbed the tree fast.', 'Black ', 'not-there');
expect(sample2).toBe('cat climbed the tree fast.');
});
it('returns empty string if start string occurs at the end', () => {
const sample = stringBetween('Black cat climbed the tree fast.', 'fast.', 'climbed');
expect(sample).toBe('');
});
it('returns empty string if start string is the entire string', () => {
const sample = stringBetween('Black cat', 'Black cat', 'climbed');
expect(sample).toBe('');
});
it('returns empty string if there is not anything between start and end', () => {
const sample = stringBetween('Black cat climbed the tree fast.', 'climbed ', 'the tree');
expect(sample).toBe('');
});
it('returns null if start string does not exist in the source string', () => {
const sample = stringBetween('Black cat climbed the tree fast.', 'not-there ', 'the tree');
expect(sample).toBe(null);
});
});
});
你也可以试试这个:
var str = 'one:two;three';
str.split(':').pop().split(';')[0]; // returns 'two'