Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #20966 > unrolled thread
| Started by | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| First post | 2013-08-17 12:56 +0000 |
| Last post | 2013-08-18 10:22 +0200 |
| Articles | 14 — 7 participants |
Back to article view | Back to comp.lang.javascript
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
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-08-17 12:56 +0000 |
| Subject | textarea multi-line regex |
| Message-ID | <kunrtr$bdr$1@dont-email.me> |
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
[toc] | [next] | [standalone]
| From | Martin Honnen <mahotrash@yahoo.de> |
|---|---|
| Date | 2013-08-17 15:10 +0200 |
| Message-ID | <520f7647$0$6638$9b4e6d93@newsspool2.arcor-online.net> |
| In reply to | #20966 |
Denis McMahon wrote:
> 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.
I think the "." dot does not match the "\n" character.
If you want to trim leading and trailing white space can't you simply use
string = string.replace(/^\s+|\s+$/g, '')
? See also http://jibbering.com/faq/#trimString.
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2013-08-17 17:13 +0200 |
| Message-ID | <XnsA21FAF439E96Deejj99@194.109.133.133> |
| In reply to | #20967 |
Martin Honnen wrote on 17 aug 2013 in comp.lang.javascript: > I think the "." dot does not match the "\n" character. [\s\S] does. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-08-18 10:57 +0200 |
| Message-ID | <2064695.sT4V1ypfN8@PointedEars.de> |
| In reply to | #20967 |
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.
[toc] | [prev] | [next] | [standalone]
| From | "Joao Rodrigues (J.R.)" <groups_jr-1@yahoo.com.br> |
|---|---|
| Date | 2013-08-19 11:04 -0300 |
| Message-ID | <kut8l8$fge$1@speranza.aioe.org> |
| In reply to | #20978 |
Em 18/08/2013 05:57, Thomas 'PointedEars' Lahn escreveu:
> Martin Honnen wrote:
>> 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>
>
<snip>
>
> 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.
Thomas, would please update the version in your site?
<http://pointedears.de/scripts/faq/cljs/#trimString>
--
Joao Rodrigues (J.R.)
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-08-17 13:18 +0000 |
| Message-ID | <kunt7j$bdr$2@dont-email.me> |
| In reply to | #20966 |
On Sat, 17 Aug 2013 12:56:27 +0000, Denis McMahon wrote: > var re = /^\s*(\S.*\S)\s*$/m; Meh, solved it now - in multiline . doesn't match newlines but \s does so: var re = /^\s*(\S[\S\s]*\S)\s*$/m; -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-08-18 10:29 +0200 |
| Message-ID | <1920170.ZjtsOj87Og@PointedEars.de> |
| In reply to | #20968 |
Denis McMahon wrote: > On Sat, 17 Aug 2013 12:56:27 +0000, Denis McMahon wrote: >> var re = /^\s*(\S.*\S)\s*$/m; > > Meh, solved it now - in multiline . doesn't match newlines “.” *never* matches newline (e.g., never "\n" or "\r"). This is why jsx.regexp.RegExp introduces PCRE's “s” modifier: <http://PointedEars.de/scripts/test/regexp> > but \s does so: “\s” matches whitespace which *includes* the newline characters "\n" and "\r". > var re = /^\s*(\S[\S\s]*\S)\s*$/m; This is pointless for matching newline. There cannot be newline in a line except at the end (where it is _not_ considered in multiline mode). If you want to match across several lines, you do _not_ want multiline mode. Curious, but true. -- PointedEars Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> |
|---|---|
| Date | 2013-08-18 19:24 +0100 |
| Message-ID | <HFA3KaViFRESFwiH@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #20968 |
In comp.lang.javascript message <kunt7j$bdr$2@dont-email.me>, Sat, 17 Aug 2013 13:18:43, Denis McMahon <denismfmcmahon@gmail.com> posted: >On Sat, 17 Aug 2013 12:56:27 +0000, Denis McMahon wrote: > >> var re = /^\s*(\S.*\S)\s*$/m; > >Meh, solved it now - in multiline . doesn't match newlines but \s does so: > >var re = /^\s*(\S[\S\s]*\S)\s*$/m; I have a recollection, which needs checking, that in a RegExp [^] matches more than . does; but that is not _necessarily_ in a JavaScript RegExp, or even anywhere true. ... ... ... <http://www.merlyn.demon.co.uk/js-valid.htm#RG> refers -- (c) John Stockton, nr London UK Reply address via Home Page. news:comp.lang.javascript FAQ <http://www.jibbering.com/faq/index.html>. <http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources. <http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2013-08-18 23:11 +0200 |
| Message-ID | <XnsA220EBF8C954Deejj99@194.109.133.133> |
| In reply to | #20987 |
Dr J R Stockton wrote on 18 aug 2013 in comp.lang.javascript:
> I have a recollection, which needs checking, that in a RegExp [^]
> matches more than . does; but that is not _necessarily_ in a JavaScript
> RegExp, or even anywhere true.
Conceptually, [^] should match any string but an empty one.
And in practice it seems to do the job:
alert(/[^]/.test(',')); // true
alert(/[^]/.test('\n')); // true
alert(/[^]/.test('')); // false
Even more interesting is [],
not matching any string, not even an empty one:
alert(/[]/.test(',')); // false
alert(/[]/.test('\n')); // false
alert(/[]/.test('')); // false
What would match only empty strings?
I mean as a regex, this one not being quite cricket:
alert(!/[^]/.test('')); // true
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-08-18 21:36 +0000 |
| Message-ID | <kureou$npc$1@dont-email.me> |
| In reply to | #20988 |
On Sun, 18 Aug 2013 23:11:49 +0200, Evertjan. wrote: > What would match only empty strings? > I mean as a regex, this one not being quite cricket: How about: /^$/ -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2013-08-19 00:50 +0200 |
| Message-ID | <XnsA22188D4E60Beejj99@194.109.133.133> |
| In reply to | #20989 |
Denis McMahon wrote on 18 aug 2013 in comp.lang.javascript: > On Sun, 18 Aug 2013 23:11:49 +0200, Evertjan. wrote: > >> What would match only empty strings? >> I mean as a regex, this one not being quite cricket: > > How about: /^$/ Elementary, my dear D. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> |
|---|---|
| Date | 2013-08-19 19:48 +0100 |
| Message-ID | <+sSpgusrhmESFwXD@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #20989 |
In comp.lang.javascript message <kureou$npc$1@dont-email.me>, Sun, 18 Aug 2013 21:36:30, Denis McMahon <denismfmcmahon@gmail.com> posted: >On Sun, 18 Aug 2013 23:11:49 +0200, Evertjan. wrote: > >> What would match only empty strings? >> I mean as a regex, this one not being quite cricket: > >How about: /^$/ <http://www.merlyn.demon.co.uk/quotings.htm#Donatus>; and /ibid/ #FrancisDrake on debugging. -- (c) John Stockton, nr London, UK. E-mail, see Home Page. Turnpike v6.05. Website <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
[toc] | [prev] | [next] | [standalone]
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Date | 2013-08-19 00:18 +0200 |
| Message-ID | <kurh8j$e0i$1@news.albasani.net> |
| In reply to | #20987 |
On 2013-08-18 20:24, Dr J R Stockton wrote: > In comp.lang.javascript message <kunt7j$bdr$2@dont-email.me>, Sat, 17 > Aug 2013 13:18:43, Denis McMahon <denismfmcmahon@gmail.com> posted: >>Meh, solved it now - in multiline . doesn't match newlines but \s does so: >> >>var re = /^\s*(\S[\S\s]*\S)\s*$/m; > > I have a recollection, which needs checking, that in a RegExp [^] > matches more than . does; but that is not _necessarily_ in a JavaScript > RegExp, or even anywhere true. That is the case in JavaScript (and ECMAScript). Unfortunately, it's a syntax error in IE8. IE9 and IE10 handle it correctly. The same goes for []. - stefan
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-08-18 10:22 +0200 |
| Message-ID | <5501177.JkyRsgVprJ@PointedEars.de> |
| In reply to | #20966 |
Denis McMahon wrote: > "/m" should mean that the whole capture works across multiple lines. No, it means that the meaning of “^” changes from “start of input” to “start of line”, and of “$” from “end of input” to “end of line”. -- PointedEars Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web