Click to See Complete Forum and Search --> : string function


tiger66
08-01-2003, 04:54 PM
Hi
I am wondering are there any string function in javascript that allow me to get the first word and ignore any words after that in a variable?

Thanks

pyro
08-01-2003, 05:00 PM
How about something like this:

<script type="text/javascript">
str = "This is a test.";
str = str.split(/\s/);
alert (str[0]);
</script>

Charles
08-01-2003, 05:00 PM
<script type="text/javascript">
<!--
String.prototype.firstWord = function () {return this.split(/\b/)[0]}

alert('Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.'.firstWord())
// -->
</script>

Charles
08-01-2003, 05:03 PM
Originally posted by pyro
How about something like this:

<script type="text/javascript">
str = "This is a test.";
str = str.split(/\s/);
alert (str[0]);
</script> That will consider punctuation as part of the preceeding word. String.split(/\b/) splits on the word boundries.

pyro
08-01-2003, 05:10 PM
True, but consider this example using \b:

<script type="text/javascript">
str = "That's a problem with \b...";
str = str.split(/\b/);
alert (str[0]);
</script>That will split it at the apostrophy, rather than the end of the word...

Charles
08-01-2003, 05:42 PM
That's because you're using a quotation mark instead of an apostrophe.

<script type="text/javascript">
<!--
String.prototype.firstWord = function () {return this.replace(/'/, '\u2019').split(/\s/)[0]}

alert('Duis\\' autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.'.firstWord())
// -->
</script>

Jeff Mott
08-01-2003, 09:10 PM
Charles
<script type="text/javascript">
<!--
String.prototype.firstWord = function () {return this.replace(/'/, '\u2019').split(/\s/)[0]}

alert('Duis\' autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.'.firstWord())
// -->
</script>:confused: With the exception of replacing single quotes with unicode right single quotation marks (basically straight quotes to curly quotes), that last script you posted now performs exactly the same as what Pyro originally posted.

Charles
08-02-2003, 07:59 AM
Originally posted by Jeff Mott
:confused: With the exception of replacing single quotes with unicode right single quotation marks (basically straight quotes to curly quotes), that last script you posted now performs exactly the same as what Pyro originally posted. There is an important distinction. Pyro is splitting words at every single white space character (splitting on /\s+/ might be better). I'm splitting on the word boundries. As I mentioned above, the difference is the punctuation. If you want the punctuation considered a part of the word then you should split on the white space and if not then you should split on the word boundries.

Jeff Mott
08-02-2003, 09:48 AM
Pyro is splitting words at every single white space character (splitting on /\s+/ might be better). I'm splitting on the word boundries.Then perhaps you had a typo. In your script which I quoted you are also splitting on white space.

Charles
08-02-2003, 12:53 PM
Originally posted by Jeff Mott
Then perhaps you had a typo. In your script which I quoted you are also splitting on white space. So I did. I apologize for the confusion.