I am trying to extract a string from within a larger string where it get everything inbetween a ':' and a ';'.
我正在尝试从一个更大的字符串中提取一个字符串,在这个字符串中,它可以得到“:”和“;”之间的所有信息。
Current
当前的
Str = 'MyLongString:StringIWant;'
Desired Output
期望输出值
newStr = 'StringIWant'
10 个解决方案
#1
231
You can try this
你可以试试这个
var mySubString = str.substring(
str.lastIndexOf(":") + 1,
str.lastIndexOf(";")
);
#2
56
You can also try this:
你也可以试试这个:
var str = 'one:two;three';
str.split(':').pop().split(';').shift(); // returns 'two'
#3
30
Use split()
使用分割()
var s = 'MyLongString:StringIWant;';
var arrStr = s.split(/[:;]/);
alert(arrStr);
arrStr
will contain all the string delimited by :
or ;
So access every string through for-loop
arrStr将包含由:或分隔的所有字符串;通过for循环访问每个字符串
for(var i=0; i<arrStr.length; i++)
alert(arrStr[i]);
#4
19
@Babasaheb Gosavi Answer is perfect if you have one occurrence of the substrings (":" and ";"). but once you have multiple occurrences, it might get little bit tricky.
@Babasaheb Gosavi的答案是完美的,如果有一个子字符串出现(“:”和“;”)。但是一旦出现了多次事件,就会变得有点棘手。
The best solution I have came up with to work on multiple projects is using four methods inside an object.
我想出的最好的解决方案是在一个对象中使用四种方法。
- First method: is to actually get a substring from between two strings (however it will find only one result).
- 第一种方法:实际上是从两个字符串之间获取子字符串(不过它只会找到一个结果)。
- Second method: will remove the (would-be) most recently found result with the substrings after and before it.
- 第二种方法:删除子字符串后和前的最近发现的结果。
- Third method: will do the above two methods recursively on a string.
- 第三个方法:在字符串上递归地执行上述两个方法。
- Fourth method: will apply the third method and return the result.
- 第四种方法:应用第三种方法并返回结果。
Code
So enough talking, let's see the 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;
}
};
How to use?
Example:
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]
#5
12
var s = 'MyLongString:StringIWant;';
/:([^;]+);/.exec(s)[1]; // StringIWant
#6
9
I like this method:
我喜欢这个方法:
var Str = 'MyLongString:StringIWant;';
var tmpStr = Str.match(":(.*);");
var newStr = tmpStr[1];
//newStr now contains 'StringIWant'
#7
1
You can also use this one...
你也可以用这个…
function extractText(str,delimiter){
if (str && delimiter){
var firstIndex = str.indexOf(delimiter)+1;
var lastIndex = str.lastIndexOf(delimiter);
str = str.substring(firstIndex,lastIndex);
}
return str;
}
var quotes = document.getElementById("quotes");
// " - represents quotation mark in HTML
<div>
<div>
<span id="at">
My string is @between@ the "at" sign
</span>
<button onclick="document.getElementById('at').innerText = extractText(document.getElementById('at').innerText,'@')">Click</button>
</div>
<div>
<span id="quotes">
My string is "between" quotes chars
</span>
<button onclick="document.getElementById('quotes').innerText = extractText(document.getElementById('quotes').innerText,'"')">Click</button>
</div>
</div>
#8
1
I used @tsds way but by only using the split function.
我使用了@tsds方法,但只使用了split函数。
var str = 'one:two;three';
str.split(':')[1].split(';')[0] // returns 'two'
#9
0
Try this to Get Substring between two characters using javascript.
尝试使用javascript在两个字符之间获取子字符串。
$("button").click(function(){
var myStr = "MyLongString:StringIWant;";
var subStr = myStr.match(":(.*);");
alert(subStr[1]);
});
Taken from @ Find substring between the two characters with jQuery
使用jQuery从@查找两个字符之间的子字符串
#10
0
Using jQuery:
使用jQuery:
get_between <- function(str, first_character, last_character) {
new_str = str.match(first_character + "(.*)" + last_character)[1].trim()
return(new_str)
}
string
字符串
my_string = 'and the thing that ! on the @ with the ^^ goes now'
usage:
用法:
get_between(my_string, 'that', 'now')
result:
结果:
"! on the @ with the ^^ goes
#1
231
You can try this
你可以试试这个
var mySubString = str.substring(
str.lastIndexOf(":") + 1,
str.lastIndexOf(";")
);
#2
56
You can also try this:
你也可以试试这个:
var str = 'one:two;three';
str.split(':').pop().split(';').shift(); // returns 'two'
#3
30
Use split()
使用分割()
var s = 'MyLongString:StringIWant;';
var arrStr = s.split(/[:;]/);
alert(arrStr);
arrStr
will contain all the string delimited by :
or ;
So access every string through for-loop
arrStr将包含由:或分隔的所有字符串;通过for循环访问每个字符串
for(var i=0; i<arrStr.length; i++)
alert(arrStr[i]);
#4
19
@Babasaheb Gosavi Answer is perfect if you have one occurrence of the substrings (":" and ";"). but once you have multiple occurrences, it might get little bit tricky.
@Babasaheb Gosavi的答案是完美的,如果有一个子字符串出现(“:”和“;”)。但是一旦出现了多次事件,就会变得有点棘手。
The best solution I have came up with to work on multiple projects is using four methods inside an object.
我想出的最好的解决方案是在一个对象中使用四种方法。
- First method: is to actually get a substring from between two strings (however it will find only one result).
- 第一种方法:实际上是从两个字符串之间获取子字符串(不过它只会找到一个结果)。
- Second method: will remove the (would-be) most recently found result with the substrings after and before it.
- 第二种方法:删除子字符串后和前的最近发现的结果。
- Third method: will do the above two methods recursively on a string.
- 第三个方法:在字符串上递归地执行上述两个方法。
- Fourth method: will apply the third method and return the result.
- 第四种方法:应用第三种方法并返回结果。
Code
So enough talking, let's see the 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;
}
};
How to use?
Example:
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]
#5
12
var s = 'MyLongString:StringIWant;';
/:([^;]+);/.exec(s)[1]; // StringIWant
#6
9
I like this method:
我喜欢这个方法:
var Str = 'MyLongString:StringIWant;';
var tmpStr = Str.match(":(.*);");
var newStr = tmpStr[1];
//newStr now contains 'StringIWant'
#7
1
You can also use this one...
你也可以用这个…
function extractText(str,delimiter){
if (str && delimiter){
var firstIndex = str.indexOf(delimiter)+1;
var lastIndex = str.lastIndexOf(delimiter);
str = str.substring(firstIndex,lastIndex);
}
return str;
}
var quotes = document.getElementById("quotes");
// " - represents quotation mark in HTML
<div>
<div>
<span id="at">
My string is @between@ the "at" sign
</span>
<button onclick="document.getElementById('at').innerText = extractText(document.getElementById('at').innerText,'@')">Click</button>
</div>
<div>
<span id="quotes">
My string is "between" quotes chars
</span>
<button onclick="document.getElementById('quotes').innerText = extractText(document.getElementById('quotes').innerText,'"')">Click</button>
</div>
</div>
#8
1
I used @tsds way but by only using the split function.
我使用了@tsds方法,但只使用了split函数。
var str = 'one:two;three';
str.split(':')[1].split(';')[0] // returns 'two'
#9
0
Try this to Get Substring between two characters using javascript.
尝试使用javascript在两个字符之间获取子字符串。
$("button").click(function(){
var myStr = "MyLongString:StringIWant;";
var subStr = myStr.match(":(.*);");
alert(subStr[1]);
});
Taken from @ Find substring between the two characters with jQuery
使用jQuery从@查找两个字符之间的子字符串
#10
0
Using jQuery:
使用jQuery:
get_between <- function(str, first_character, last_character) {
new_str = str.match(first_character + "(.*)" + last_character)[1].trim()
return(new_str)
}
string
字符串
my_string = 'and the thing that ! on the @ with the ^^ goes now'
usage:
用法:
get_between(my_string, 'that', 'now')
result:
结果:
"! on the @ with the ^^ goes