Javascript regexp help needed
March 22, 2006 11:30 AM   Subscribe

Using javascript, what would be the most efficient way to reduce a multi-word string down to just the first word?

The site takes a "first name/mi/last name" submission all in one field, rather than separate fields for each. I want to grab just the first name out of the submitted field. I realize the best solution would be to change the form to separate the fields but that's not an option at the moment.
posted by mr_crash_davis to Computers & Internet (10 answers total)
 
If the names are space-separated, you can use the split() method of the String class:

firstname = fullname.split(" ")[0];
posted by mbrubeck at 11:44 AM on March 22, 2006


Probably the better way is to handle it server-side, so that in case users have javascript turned off they'll still be able to submit the form.
posted by ChasFile at 12:06 PM on March 22, 2006


What Chas said.

But if you do what brubeck said, realize his example is actually splitting all the tokens, and just throwing away all but the first. Since you want first last and middle, you can do this:

var a = fullname.split(" ") ;
var first = a.shift() ;
var last = a.pop() ;
var middle = a.join(" " ) ;
posted by orthogonality at 12:17 PM on March 22, 2006


Another way, using regular expressions:

var poem = ' April is the cruelest month. ';
var firstWord = poem.replace(/[^\w]*(\w+).*/,"$1");

posted by ijoshua at 2:19 PM on March 22, 2006


Regex requires a state machine, gonna be more expensive that scaning for a char.
posted by orthogonality at 2:22 PM on March 22, 2006


Yeah, the problem with all of these is if someone enters a first name that has a space in it (e.g., "Virginia Grace", etc.) -- people use dual names as what others would colloquially think of as their first name, and you'll only be getting part of it.

So I'd say, if you *do* separate based on spaces, then you should try to use some sort of validation to ask the user if you've made the right choice.
posted by delfuego at 2:23 PM on March 22, 2006


Best answer: You don't need a regex or a split, you can just do an indexOf/substring:
str="john smith";firstspaceloc = str.indexOf(' ');firstname = str.substr(0,firstspaceloc);alert(firstname);
should give you "john".

String operations are always quicker than regexes, surely, and better supported in older browsers too.
posted by AmbroseChapel at 2:56 PM on March 22, 2006


Best answer: Can't ... stop ... self ... must optimise ...
str="john smith";
alert(str.substr(0,str.indexOf(' ')));

posted by AmbroseChapel at 3:00 PM on March 22, 2006


Probably going to want to trim that string first, lest you wind up with nothing. I'd echo what others have said: none of these methods is going to be foolproof, so make sure you're ok with a large margin of error.
posted by yerfatma at 3:20 PM on March 22, 2006


To deal with multiple first names, seperate names with slashes rather that spaces ex: Virginia Grace/A/Smith and do what AmbroseChapel suggested except replace str.indexOf(" ") with str.indexOf("/")
posted by martinX's bellbottoms at 7:33 PM on March 22, 2006


« Older Hotel or B&B in Vancouver   |   Movies that show the grim consequences of quantum... Newer »
This thread is closed to new comments.