Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #20966

textarea multi-line regex

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.javascript
Subject textarea multi-line regex
Date 2013-08-17 12:56 +0000
Organization A noiseless patient Spider
Message-ID <kunrtr$bdr$1@dont-email.me> (permalink)

Show all headers | View raw


I'm trying to trim whitespace from the text in a textarea:

I assume that there are at least two non whitespace characters in the 
textarea.

function trim( id ) {

var re = /^\s*(\S.*\S)\s*$/m;

var cap = re.exec( document.getElementById( id ).value );

if ( cap.length == 2 ) document.getElementById( id ).value = cap[1];

}

Now I think that:

the first "\s*" should capture all whitespace before the first non-
whitespace character;

the second "\s*" should capture all whitespace after the last non-
whitespace character;

the "(\S.*\S)" should capture ereything except the leading and trailing 
whitespace (from the first non whitespace character to the last non 
whitespace character inclusive);

"/m" should mean that the whole capture works across multiple lines.

However, the textarea is set to the trimmed first line of the original 
only.

Any ideas why multiline matching isn't working the way I expect? I 
suspect that the ".*" in the middle of "(\S.*\S)" isn't being greedy 
across line breaks.

Example page:

http://www.sined.co.uk/tmp/textareatrim.htm

-- 
Denis McMahon, denismfmcmahon@gmail.com

Back to comp.lang.javascript | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

textarea multi-line regex Denis McMahon <denismfmcmahon@gmail.com> - 2013-08-17 12:56 +0000
  Re: textarea multi-line regex Martin Honnen <mahotrash@yahoo.de> - 2013-08-17 15:10 +0200
    Re: textarea multi-line regex "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-08-17 17:13 +0200
    Re: textarea multi-line regex Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-18 10:57 +0200
      Re: textarea multi-line regex "Joao Rodrigues (J.R.)" <groups_jr-1@yahoo.com.br> - 2013-08-19 11:04 -0300
  Re: textarea multi-line regex Denis McMahon <denismfmcmahon@gmail.com> - 2013-08-17 13:18 +0000
    Re: textarea multi-line regex Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-18 10:29 +0200
    Re: textarea multi-line regex Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-08-18 19:24 +0100
      Re: textarea multi-line regex "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-08-18 23:11 +0200
        Re: textarea multi-line regex Denis McMahon <denismfmcmahon@gmail.com> - 2013-08-18 21:36 +0000
          Re: textarea multi-line regex "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-08-19 00:50 +0200
          Re: textarea multi-line regex Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-08-19 19:48 +0100
      Re: textarea multi-line regex Stefan Weiss <krewecherl@gmail.com> - 2013-08-19 00:18 +0200
  Re: textarea multi-line regex Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-18 10:22 +0200

csiph-web