I have been looking everywhere and can't find this..
I got a string and I want to replace the whole string with only the first word.
for example:
String: "hello world"
New String: "hello"
Thanks in advance!
I have been looking everywhere and can't find this..
I got a string and I want to replace the whole string with only the first word.
for example:
String: "hello world"
New String: "hello"
Thanks in advance!
Share Improve this question edited Oct 24, 2011 at 18:58 Michael Low 24.5k17 gold badges85 silver badges119 bronze badges asked Oct 24, 2011 at 18:51 GermanManGermanMan 1031 silver badge11 bronze badges 02 Answers
Reset to default 6var s = "hello world"
s = s.replace(/(\w+).*/,"$1");
that'll do it.
The quickest and easiest way is to split the string into an array based on spaces, then set it to be the first one.
var theString = "hello world";
theString = theString.split(" ")[0];
alert(theString); // alerts "hello"