J'essaie d'extraire une chaîne d'une chaîne plus grande où elle obtient tout entre un ':' et un ';'.
Actuel
Str = 'MyLongString:StringIWant;'
Sortie souhaitée
newStr = 'StringIWant'
Vous pouvez essayer ceci
var String=str.substring(str.lastIndexOf(":")+1,str.lastIndexOf(";"));
Vous pouvez également essayer ceci:
var str = 'one:two;three'; str.split(':').pop().split(';').shift(); // returns 'two'
Utiliser split()
var s = 'MyLongString:StringIWant;'; var arrStr = s.split(/[:;]/); alert(arrStr);
arrStr
contiendra toute la chaîne délimitée par :
ou ;
Donc, accédez à toutes les chaînes dans la for-loop
for(var i=0; i<arrStr.length; i++) alert(arrStr[i]);
var s = 'MyLongString:StringIWant;'; /:([^;]+);/.exec(s)[1]; // StringIWant
J'aime cette méthode:
var Str = 'MyLongString:StringIWant;'; var tmpStr = Str.match(":(.*);"); var newStr = tmpStr[1]; //newStr now contains 'StringIWant'
@Babasaheb Gosavi La réponse est parfaite si vous avez une occurrence des sous-chaînes (":" et ";"). Mais une fois que vous avez plusieurs occurrences, cela pourrait être un peu délicat.
La meilleure solution pour laquelle j'ai travaillé sur plusieurs projets utilise quatre méthodes dans un objet.
Donc assez de parler, voyons le 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]
Vous pouvez également utiliser celui-ci …
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>