In one of the previous articles, I mentioned Get Substring after character in Javascript now in this article, I have mentioned how we can Get string after specific word or after substring using JavaScript in various possible ways (Using Substring, using Regex or using Split)

get-string-after-word-javascript

Using String.substring method

const originalString = "This is a sample sentence in JavaScript.";
const specificWord = "sample";

const substring = originalString.substring(originalString.indexOf(specificWord) + specificWord.length);

console.log(substring);
//output => sentence in JavaScript.

In the above code, we are using substring(position, length) method to get remaining string after a word.

Using String.split method

const originalString = "This is a sample sentence in JavaScript.";
const specificWord = "sample";

const wordsArray = originalString.split(" ");
const indexOfWord = wordsArray.indexOf(specificWord);

const substring = wordsArray.slice(indexOfWord + 1).join(" ");

console.log(substring);

In the above code, we first split the complete string by " " (space) using .split(" ")

Then once we have splitted string array, we get array index of word

Then we get splitted array values, after specific word index

Using Regular Expressions (RegEx)

const originalString = "This is a sample sentence in JavaScript.";
const specificWord = "sample";

const regex = new RegExp(`\\b${specificWord}\\b(\\s+)?(.*)`);
const match = originalString.match(regex);

const substring = match ? match[2] : "";

console.log(substring);

In the above javascript code, we are using Regex to find specificword

Once we have match, we get last array of words (string) from matched value.

You may also like to read:

Find Element based on Data-Attribute using Javascript or jQuery

Various ways to convert date to Unix Timestamp in Javascript

How can I check Typescript version in Visual Studio?

Various ways to make HTTP Request in Javascript