简体   繁体   中英

Get substring after first space and before first instance of a special character?

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) ...

Not much different:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM