2

I am successfully returning a substring before the first instance of a space in a string:

var str = "test1 test2 test3 (test4)";
var str_BeforeSpace = str.substr(0,str.indexOf(' '));
// returns "test1"

What I'm trying to do next is return a substring that exists after the first space and before the first instance of a "(". In this example, the desired substring is "test2 test3" (with no trailing spaces) ...

faalbane
  • 81
  • 1
  • 8

1 Answers1

2

Not much different:

str.substring(str.indexOf(' ') + 1, str.indexOf('('));

If you want to trim leading and trailing spaces, see Trim string in JavaScript? .

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143