javascript - Slice a string from second space to a special character? -
var name_rawstring = 'a. john doe-john (class of 2010)';
i'm trying extract substring occuring after second space , before (
. in case: "doe-john".
so far i've tried:
var getstr = name_rawstring.slice(name_rawstring.substr(0, name_rawstring.indexof(' ', name_rawstring.indexof(' ') + 1)), name_rawstring.indexof(' ('));
and
var strsplit = name_rawstring.split(' '); var getstr = name_rawstring.slice(name_rawstring.indexof(strsplit[1]) + 1)), name_rawstring.indexof(' ('));
you give regular expressions try:
name_rawstring.match(/^(?:\s+\s){2}(.+?) \(/)[1];
or, if prefer more verbose solution:
var haystack = 'a. john doe-john (class of 2010)'; var firstspaceindex = haystack.indexof(' '); var secondspaceindex = haystack.indexof(' ', firstspaceindex + 1); var openparenindex = haystack.indexof('('); var needle = haystack.slice(secondspaceindex + 1, openparenindex - 1);
Comments
Post a Comment