OneCompiler

[Javascript] How to extract the substring present between two characters?

I want to extract a substring present between two characters, How can I do that in Javascript?

1 Answer

4 years ago by

Using substring() method you can extract the substring present between two characters. To identify the index of the characters, you can use indexOf() or lastIndexOf() methods.

let str = "Hello, World!";

console.log(str.substring( str.indexOf(",") + 1, str.lastIndexOf("!")));

Run here https://onecompiler.com/javascript/3xnrrhegb

4 years ago by Meera