javascript - Given a paragraph of text, how to get everything from the start to the last whitespace? -



javascript - Given a paragraph of text, how to get everything from the start to the last whitespace? -

given block of text so:

"hello world http://yahoo.com "

on keypress i'm looking @ text links. don't want links in text while user still typing, example:

"hello world http://yahoo.co"

how can in text var starting left, 0, way lastly white space, meaning don't url while user typing it?

thanks

updated

typing hello world http://yahoo.com should homecoming hello world http://yahoo.com

typing hello world http://yahoo.co should homecoming hello world is

two methods:

first, regular expression:

var str = "hello world http://yahoo.co", expr = /(.+\s).*$/ result = str.match( expr ) ; console.log( result ); // => ["hello world http://yahoo.co", "hello world "] console.log( result[1] ); // => "hello world "

second, lastindexof:

var str = "hello world http://yahoo.co", lastidx = str.lastindexof( ' ' ) result = str.substr( 0, lastidx + 1 ) ; console.log( result ); // => "hello world "

this latter works spaces, however, lastindexof doesn't take regular expression.

javascript jquery

Comments

Popular posts from this blog

delphi - blogger via idHTTP : error 400 bad request -

c++ - compiler errors when initializing EXPECT_CALL with function which has program_options::variables_map as parameter -

How do I check if an insert was successful with MySQLdb in Python? -