Split the string between the two characters?

Viewed 38

I had the following string ,

He is @(role)

I need to get the string which is present between @( and ).

Expected result ,

role

4 Answers

We can use match() here:

var input = "He is @(role)";
var role = input.match(/@\((.*?)\)/)[1];
console.log(role);

You have to use the split method of string which split the string where you want, answer of the question here:

const string = 'He is @(role)';

const answer = string.split('@');

if you console.log(answer) then the console show

['He is ', '(role)'];

const arrMatch = 'He is @(role)'.match(/\@\(([^\)]+)/)
const text = arrMatch[1]
console.log(text)

Related