I've this:
"example: myresult"
And I need a method that looks for this from ": "
"myresult"
I tried with String.search()
CodePudding user response:
use string.includes(searchString, position)
, that have a second param of position to start the search.
position Optional The position within the string at which to begin searching for searchString. (Defaults to 0.)
const str = "example: myresult";
const startIndex = str.indexOf(': ');
console.log(str.includes('example')); // true
console.log(str.includes('example', startIndex)); // false
console.log(str.includes('myresult', startIndex)); //true
CodePudding user response:
this should work
function getSecondPart(str) {
return str.split(':')[1];
}
CodePudding user response:
As a last resort you have also the regular expression route to extract the value following semicolons preceded by a label:
const subject = "example: myresult";
const re = /^. ?\: (. )$/im;
const match = re.exec(subject);
let result = "";
if (match !== null) {
result = match[1];
}
console.log(result);
CodePudding user response:
Assuming the ": " is always consistent this should work for you:
var myresult = "example: myresult";
console.log(myresult.split(': ')[1]);
CodePudding user response:
try
'string'.includes(':')
you can check if the symbol or letter is in the word by includes in the words