I have a string that contains the following :
a test here as well .... Test: 1- (link) 2- (link)
and i want to search for Test
and get what it follows after it.
i tried string.includes("Test")
but it only returns true or false
I have a string that contains the following :
a test here as well .... Test: 1- (link) 2- (link)
and i want to search for Test
and get what it follows after it.
i tried string.includes("Test")
but it only returns true or false
- 1 please share the plete string – brk Commented Mar 13, 2019 at 13:36
-
Just use
indexof
instead ofincludes
and if > -1 then use that + length of string tosubstr
anything after it. – JBC Commented Mar 13, 2019 at 13:40
7 Answers
Reset to default 7You could match the wanted word and take all characters after.
var string = 'a test here as well .... Test: 1- (link) 2- (link)',
part = string.match(/Test(.*$)/)[1];
console.log(part);
If the string is likely not to match, you could add a default array for a null
value and get undefied
instead of a nonmatched part.
var string = 'a test here as well .... Test: 1- (link) 2- (link)',
part = (string.match(/TestX(.*$)/) || [])[1];
console.log(part);
A simple way to do this is to split()
the Sting on the text you want and the result[1] will be the text after the splitting string.
so...
var s = 'a test here as well .... Test: 1- (link) 2- (link)';
var splitText = 'Test'
var result = s.split(splitText)[1];
Hope that helps.
- First you need to get index of the element that you searched: indexOf
- After that you can get extracted text that includes your keyword using slice method.
I have also created a demo that helps you to understand.
const getAfterText = (allText, keyword) => {
return allText.slice(allText.indexOf(keyword));
};
You may use a capture group inside a regular expression to capture everything after the matched pattern (your string). Below tests if you found it, if you did the value will be stored in the $1
of the RegExp
object.
const str = 'a test here as well .... Test: 1- (link) 2- (link)'
if ( /Test(.*)$/.test(str) )
console.log(RegExp.$1)
Here is another way to functionalize the above:
const text = 'a test here as well .... Test: 1- (link) 2- (link)'
console.log( trailing(text, 'Test') )
function trailing(str, pattern){
const re = new RegExp(`${pattern}(.*)$`)
if ( re.test(str) )
return RegExp.$1.toString()
return '' // false and default condition
}
Simpliest way to do it:
const yourString = "a test here as well .... Test: 1- (link) 2- (link)";
const searchTerm = "Test";
const result = yourString.split(searchTerm).at(1);
You can get the index of word and then get the substring.
let str = 'a test here as well .... Test: 1- (link) 2- (link)',
word = 'Test',
substring = '';
if(str.indexOf(word) > -1) {
substring = str.substr(str.indexOf(word) + word.length);
}
console.log(substring);
I believe lastIndexOf
and substr
is a easy fit for your case:
let text = 'a test here as well .... Test: 1- (link) 2- (link)'
let position = text.lastIndexOf('Test: ')
let result = position > -1 ? text.substr(position + 6) : ''
console.log(result)