-3

I had the following string ,

He is @(role)

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

Expected result ,

role

AsZik
  • 55
  • 4

4 Answers4

0

We can use match() here:

var input = "He is @(role)";
var role = input.match(/@\((.*?)\)/)[1];
console.log(role);
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

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)'];

0

const arrMatch = 'He is @(role)'.match(/\@\(([^\)]+)/)
const text = arrMatch[1]
console.log(text)
Steve Tomlin
  • 3,391
  • 3
  • 31
  • 63
0

here is the best and simple way to do this with string slice method which helps you to splice the exact string which you want. Here is the answer

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

const answer = string.slice(8, 12);

console.log(answer);

Now you can see the result 'role'

Explain: here is the resource which helps you learn this method

https://www.w3schools.com/jsref/jsref_slice_string.asp#:~:text=The%20slice()%20method%20extracts,of%20the%20string%20to%20extract.