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
Post a Comment