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


Groups > comp.lang.javascript > #20978

Re: textarea multi-line regex

Message-ID <2064695.sT4V1ypfN8@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-08-18 10:57 +0200
Subject Re: textarea multi-line regex
Newsgroups comp.lang.javascript
References <kunrtr$bdr$1@dont-email.me> <520f7647$0$6638$9b4e6d93@newsspool2.arcor-online.net>

Show all headers | View raw


Martin Honnen wrote:

> I think the "." dot does not match the "\n" character.

Correct.

> If you want to trim leading and trailing white space can't you simply use
>     string = string.replace(/^\s+|\s+$/g, '')

  string = string.replace(/^\s+/, '').replace(/\s+$/, '');

might be more efficient:

<http://blog.stevenlevithan.com/archives/faster-trim-javascript>

ECMAScript Edition 5 introduces String.prototype.trim():

  string = string.trim();

<http://PointedEars.de/es-matrix/?filter=trim>
<http://ecma-international.org/ecma-262/5.1/#sec-15.5.4.20>
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim>

The following pattern appears to be useful:

  string = (typeof string.trim == "function"
    ? string.trim()
    : string.replace(/^\s+/, '').replace(/\s+$/, ''));

You may want to emulate the “trim” method if you cannot be sure it is 
implemented, and you need to trim more than one time:

  if (typeof String.prototype.trim != "function")
  {
    String.prototype.trim = function () {
      return this.replace(/^\s+/, '').replace(/\s+$/, '');
    };
  }

  // …

  string = string.trim();

(Emulations backwards compatible to the earliest versions of ECMAScript 
implementations, and beyond, are also conceivable.)

> ? See also http://jibbering.com/faq/#trimString.

An update there is indicated.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | 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