Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #15593 > unrolled thread
| Started by | Tim Streater <timstreater@greenbee.net> |
|---|---|
| First post | 2012-08-28 15:51 +0100 |
| Last post | 2012-08-31 23:04 +0100 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.javascript
regexp help needed Tim Streater <timstreater@greenbee.net> - 2012-08-28 15:51 +0100
Re: regexp help needed Stefan Weiss <krewecherl@gmail.com> - 2012-08-28 17:32 +0200
Re: regexp help needed Tim Streater <timstreater@greenbee.net> - 2012-08-28 16:41 +0100
Re: regexp help needed Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-08-28 19:07 +0200
Re: regexp help needed SAM <stephanemoriaux.NoAdmin@wanadoo.fr.invalid> - 2012-08-28 19:54 +0200
Re: regexp help needed Dr J R Stockton <reply1235@merlyn.demon.co.uk.invalid> - 2012-08-29 19:16 +0100
Re: regexp help needed Tim Streater <timstreater@greenbee.net> - 2012-08-29 23:51 +0100
Re: regexp help needed Dr J R Stockton <reply1235@merlyn.demon.co.uk.invalid> - 2012-08-31 23:04 +0100
| From | Tim Streater <timstreater@greenbee.net> |
|---|---|
| Date | 2012-08-28 15:51 +0100 |
| Subject | regexp help needed |
| Message-ID | <timstreater-EBD02F.15513028082012@news.individual.net> |
I try and avoid regexp as much as possible, but there are times when one
such should represent a simple solution.
I have a string that may contain one or more single quotes, and for
subsequent use of the string I want to escape these. So, if I have a
string such as:
my cat's name is Fred
then I'd like to replace that with:
my cat\'s name is Fred
With my source string in the JavaScript variable str, then I've tried
such as:
str.replace (/'/g, "\\'");
but all of my attempts have zero effect. This must be simple enough :-)
but I'm missing something, so I'd appreciate a helping hand.
Thanks,
--
Tim
"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
[toc] | [next] | [standalone]
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Date | 2012-08-28 17:32 +0200 |
| Message-ID | <k1iob5$rsh$1@news.albasani.net> |
| In reply to | #15593 |
On 2012-08-28 16:51, Tim Streater wrote:
> So, if I have a string such as:
>
> my cat's name is Fred
>
> then I'd like to replace that with:
>
> my cat\'s name is Fred
>
> With my source string in the JavaScript variable str, then I've tried
> such as:
>
> str.replace (/'/g, "\\'");
>
> but all of my attempts have zero effect. This must be simple enough :-)
> but I'm missing something, so I'd appreciate a helping hand.
This should work, but replace() doesn't change the string itself - it
returns a new string. Maybe you didn't assign the result back to str?
str = str.replace(/'/g, "\\'");
print(str);
// my cat\'s name is Fred
- stefan
[toc] | [prev] | [next] | [standalone]
| From | Tim Streater <timstreater@greenbee.net> |
|---|---|
| Date | 2012-08-28 16:41 +0100 |
| Message-ID | <timstreater-9C24C2.16411128082012@news.individual.net> |
| In reply to | #15594 |
In article <k1iob5$rsh$1@news.albasani.net>, Stefan Weiss <krewecherl@gmail.com> wrote: > On 2012-08-28 16:51, Tim Streater wrote: > > So, if I have a string such as: > > > > my cat's name is Fred > > > > then I'd like to replace that with: > > > > my cat\'s name is Fred > > > > With my source string in the JavaScript variable str, then I've tried > > such as: > > > > str.replace (/'/g, "\\'"); > > > > but all of my attempts have zero effect. This must be simple enough :-) > > but I'm missing something, so I'd appreciate a helping hand. > > This should work, but replace() doesn't change the string itself - it > returns a new string. Maybe you didn't assign the result back to str? > > str = str.replace(/'/g, "\\'"); > print(str); > // my cat\'s name is Fred Thanks Stefan. That was it, of course. My fault for being too hasty. -- Tim "That excessive bail ought not to be required, nor excessive fines imposed, nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-08-28 19:07 +0200 |
| Message-ID | <1530192.JQZcuHLreY@PointedEars.de> |
| In reply to | #15594 |
Stefan Weiss wrote:
> str = str.replace(/'/g, "\\'");
> print(str);
> // my cat\'s name is Fred
JFTR, in this case one might want to use the more distinct
str = str.replace(/'/g, "\\$&");
instead. "$&" is equivalent to the matched substring, which comes in handy
for more general replacements:
/* from JSX:regex.js [1] */
jsx.regexp.escape = function (s) {
// ...
return s.replace(/[\]\\^$*+?.(){}[]/g, "\\$&");
};
RTFineM for more about replacement sequences [2].
PointedEars
___________
[1]
<http://pointedears.de/websvn/filedetails.php?repname=JSX&path=%2Ftrunk%2Fregexp.js&rev=252>
[2] <https://developer.mozilla.org/en-
US/docs/JavaScript/Reference/Global_Objects/String/replace>
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
[toc] | [prev] | [next] | [standalone]
| From | SAM <stephanemoriaux.NoAdmin@wanadoo.fr.invalid> |
|---|---|
| Date | 2012-08-28 19:54 +0200 |
| Message-ID | <503d05e5$0$6199$ba4acef3@reader.news.orange.fr> |
| In reply to | #15593 |
Le 28/08/12 16:51, Tim Streater a écrit :
> I try and avoid regexp as much as possible, but there are times when one
> such should represent a simple solution.
>
> I have a string that may contain one or more single quotes, and for
> subsequent use of the string I want to escape these. So, if I have a
> string such as:
>
> my cat's name is Fred
>
> then I'd like to replace that with:
>
> my cat\'s name is Fred
>
> With my source string in the JavaScript variable str, then I've tried
> such as:
>
> str.replace (/'/g, "\\'");
you habe to be sure that :
- str is really a string
- str contains all your text
(the ' to change wasn't it token for the end of the string ?)
> but all of my attempts have zero effect. This must be simple enough :-)
I do not understand ...
alert("my cat's name is Fred".replace(/'/g,"\'"))
works fine for me
--
Stéphane Moriaux avec/with iMac-intel
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1235@merlyn.demon.co.uk.invalid> |
|---|---|
| Date | 2012-08-29 19:16 +0100 |
| Message-ID | <QE9ropMyxlPQFwxP@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #15593 |
In comp.lang.javascript message <timstreater-EBD02F.15513028082012@news.
individual.net>, Tue, 28 Aug 2012 15:51:30, Tim Streater
<timstreater@greenbee.net> posted:
>I try and avoid regexp as much as possible, but there are times when
>one such should represent a simple solution.
For string manipulation independent of numerical values, a RegExp
solution is commonly best; so an author should use RegExps wherever he
conveniently can.
>I have a string that may contain one or more single quotes, and for
>subsequent use of the string I want to escape these. So, if I have a
>string such as:
You know the answer.
But IMHO your first move should have been to use an entirely un-
contentious character, such as 5, instead of the quote, and test on such
as
my cat5s name
to check whether there is something special about the quote character.
--
(c) John Stockton, nr London, UK. Mail via homepage. Turnpike v6.05 MIME.
Web <http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms and links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
[toc] | [prev] | [next] | [standalone]
| From | Tim Streater <timstreater@greenbee.net> |
|---|---|
| Date | 2012-08-29 23:51 +0100 |
| Message-ID | <timstreater-42655B.23515129082012@news.individual.net> |
| In reply to | #15611 |
In article <QE9ropMyxlPQFwxP@invalid.uk.co.demon.merlyn.invalid>, Dr J R Stockton <reply1235@merlyn.demon.co.uk.invalid> wrote: > In comp.lang.javascript message <timstreater-EBD02F.15513028082012@news. > individual.net>, Tue, 28 Aug 2012 15:51:30, Tim Streater > <timstreater@greenbee.net> posted: > > >I try and avoid regexp as much as possible, but there are times when > >one such should represent a simple solution. > > For string manipulation independent of numerical values, a RegExp > solution is commonly best; so an author should use RegExps wherever he > conveniently can. Convenience is the keyword. Most regexps look like line-noise to me, so I avoid them for all except the most simple cases. > >I have a string that may contain one or more single quotes, and for > >subsequent use of the string I want to escape these. So, if I have a > >string such as: > > You know the answer. I do now, yes. > But IMHO your first move should have been to use an entirely un- > contentious character, such as 5, instead of the quote, and test on such > as > my cat5s name > to check whether there is something special about the quote character. Why would I want to do that? I already had the problem with the quote as I was building an href in JavaScript for which I was using ' as delimiter. My mistake was not to consider that the string I was using as part of the href might contain a quote. -- Tim "That excessive bail ought not to be required, nor excessive fines imposed, nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1235@merlyn.demon.co.uk.invalid> |
|---|---|
| Date | 2012-08-31 23:04 +0100 |
| Message-ID | <iQXnNrFkTTQQFwv0@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #15612 |
In comp.lang.javascript message <timstreater-42655B.23515129082012@news.
individual.net>, Wed, 29 Aug 2012 23:51:51, Tim Streater
<timstreater@greenbee.net> posted:
>
>> But IMHO your first move should have been to use an entirely un-
>> contentious character, such as 5, instead of the quote, and test on such
>> as
>> my cat5s name
>> to check whether there is something special about the quote character.
>
>Why would I want to do that? I already had the problem with the quote
>as I was building an href in JavaScript for which I was using ' as
>delimiter. My mistake was not to consider that the string I was using
>as part of the href might contain a quote.
Indeed. But, when you did not know what the problem was, you might not
have rightly known whether the single quote character was special in a
RegExp. When one does not understand a problem, it can be useful to try
such simple and easy tests to simplify subsequent thinking. If you had
made the RegExp, about which you asked for help, so simple as to be
infallible (albeit not then the RegExp you needed), you would have been
driven to consider why the result was not appearing where expected.
Years ago, I had a Pascal program (on a 12MHz HP PC, IIRC) of many
fairly large units, constantly being edited, and it suddenly started to
give a peculiar error message (I forget what). Eventually I resorted to
simplification, rather hoping that I would thereby find, first, in which
unit it lay.
I ended up with a single-line program still giving the message,
something like
begin var X : real ; X := sqrt(2) ; end.
At least that eliminated the rest of my program ; and removing the sqrt
call removed the error message.
It turned out that a new 4-port serial I/O board, not involved in the
program, had a defective address decoder, so that it responded during
exchanges between the CPU and its external FPU (80286 & 80287?),
corrupting the dialogue.
--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk 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] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web