-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
  • 28,232
  • 5
  • 37
  • 57
Cuheguevara
  • 122
  • 1
  • 1
  • 14
  • StackOverflow is not a free coding service. SO expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592). Please update your question to show what you have already tried in a [mcve]. For further information, please see [ask], and take the [tour] :) You should read a regexp tutorial. – Barmar Aug 14 '18 at 03:05
  • @Barmar, I'm sorry for my question, I was chased by the time, so I didn't have time to read about regex, I was a beginner in the regex. – Cuheguevara Aug 14 '18 at 03:09

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
  • 22,351
  • 6
  • 45
  • 63
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
  • 14,151
  • 2
  • 21
  • 24
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)