-2

I have string like this:

var str = "Hello (World) I'm Newbie";

how to get World from string above using RegExp?, I'm sorry I don't understand about regex.

Thank's

Dacre Denny
  • 29,664
  • 5
  • 45
  • 65

3 Answers3

2

Assuming that there will be atleast one such word, you can do it using String#match. The following example matches the words between parentheses.

console.log(
  "Hello (World) I'm Newbie"
  .match(/\(\w+\)/g)
  .map(match => match.slice(1, -1))
)
31piy
  • 23,323
  • 6
  • 47
  • 67
2

Rather than using a regex - use .split()...Note the escaped characters in the splits. The first split gives "World) I'm Newbie" and the second gives "World".

var str = "Hello (World) I'm Newbie";

var strContent = str.split('\(')[1].split('\)')[0];
console.log(strContent); // gives "World"
gavgrif
  • 15,194
  • 2
  • 25
  • 27
0

This might help you for your regex

  1. \w match whole world
  2. + plus with another regex
  3. [] starts group
  4. ^ except
  5. (World) matching word

var str = "Hello (World) I'm Newbie";
var exactword=str.replace(/\w+[^(World)]/g,'')
var filtered = str.replace(/(World)/g,'') 
alert(exactword)
alert(filtered)