Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #9348 > unrolled thread
| Started by | Jason C <jwcarlton@gmail.com> |
|---|---|
| First post | 2011-12-17 02:42 -0800 |
| Last post | 2011-12-17 18:31 +0100 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.javascript
String.replace() with regex Jason C <jwcarlton@gmail.com> - 2011-12-17 02:42 -0800
Re: String.replace() with regex Stefan Weiss <krewecherl@gmail.com> - 2011-12-17 14:08 +0100
Re: String.replace() with regex Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-12-17 14:12 +0100
Re: String.replace() with regex Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2011-12-17 18:31 +0100
| From | Jason C <jwcarlton@gmail.com> |
|---|---|
| Date | 2011-12-17 02:42 -0800 |
| Subject | String.replace() with regex |
| Message-ID | <16483917.24.1324118524679.JavaMail.geo-discussion-forums@yqlp16> |
I have this regex, which works as expected: a = a.replace(/ itxtHarvested="(.*?)"/i, ""); I modified it to this: a = a.replace(/\s*itxtHarvested="*(.*?)"*/i, ""); which I thought would be the same, but would make the /s and " optional. But, in practice, instead of just removing it, it was being converted to something like: 441?0? (Or maybe 530"0"; I had made several modifications using the *, and now I don't remember which error went where.) Does the * not mean "0 or more times" in Javascript like it does in other languages? I know that a few things are different with JS (like the /s modifier), so I wasn't sure. Or, is it just something flawed in my logic that made it not work as expected?
[toc] | [next] | [standalone]
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Date | 2011-12-17 14:08 +0100 |
| Message-ID | <LsadnYc-WN0gCXHTnZ2dnUVZ8vCdnZ2d@giganews.com> |
| In reply to | #9348 |
On 2011-12-17 11:42, Jason C wrote: > I have this regex, which works as expected: > > a = a.replace(/ itxtHarvested="(.*?)"/i, ""); > > I modified it to this: > > a = a.replace(/\s*itxtHarvested="*(.*?)"*/i, ""); > > which I thought would be the same, but would make the /s and " > optional. But, in practice, instead of just removing it, it was being > converted to something like: > > 441?0? > > (Or maybe 530"0"; I had made several modifications using the *, and > now I don't remember which error went where.) You didn't post what type of input string you're matching against. Based on the variable name and the regex, I'd assumed it would be an HTML anchor tag, but then where are those numbers coming from? If you want to make a single character optional, you usually use "?" instead of "*", unless you want to allow for an endless repetition of that character. What your regex is looking for is a) as much whitespace as possible (including none) b) the string 'itxtHarvested=' c) as many double quotes as possible (including none) d) as few characters as possible e) as many double quotes as possible (including none) With an input like <a href="blarg" itxtHarvested="blimm"> the double quote after 'itxtHarvested=' will be the best match for c-e, so the result will be <a href="blarg"blimm"> > Does the * not mean "0 or more times" in Javascript like it does in > other languages? I know that a few things are different with JS (like > the /s modifier), so I wasn't sure. > > Or, is it just something flawed in my logic that made it not work as > expected? The regex does the same thing in Perl and other languages. Interpreting your question freely as "how can I parse XML/HTML attribute values with regular expressions?", I feel obliged to warn you that any number of Bad Things can happen when you try that [0]. Warnings aside, in well-formed HTML/XML you're basically looking at three variations: double quoted, single quoted, and unquoted attribute values. The first two can be matched with /="[^"]*"/ and /='[^']*'/, respectively, and the last (sometimes, in good weather, and with the wind at your back) with /=\S+/. - stefan [0] http://stackoverflow.com/questions/1732348
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2011-12-17 14:12 +0100 |
| Message-ID | <1929900.aqSPkdTlGX@PointedEars.de> |
| In reply to | #9348 |
Jason C wrote:
> I have this regex, which works as expected:
>
> a = a.replace(/ itxtHarvested="(.*?)"/i, "");
>
> I modified it to this:
>
> a = a.replace(/\s*itxtHarvested="*(.*?)"*/i, "");
>
> which I thought would be the same, but would make the /s and " optional.
How can it be the same then? It would make leading white-space, and
optional in that it would allow 0 or more of them, including `"""""'.
(This is probably not what you want.)
> But, in practice, instead of just removing it, it was being converted to
> something like:
>
> 441?0?
>
> (Or maybe 530"0"; I had made several modifications using the *, and now I
> don't remember which error went where.)
No, it was not.
> Does the * not mean "0 or more times" in Javascript like it does in other
> languages?
There is no "Javascript": <http://PointedEars.de/es-matrix>
The `*' means the same in ECMAScript implementations as in other programming
languages.
> I know that a few things are different with JS (like the /s
> modifier), so I wasn't sure.
ECMAScript Regular Expressions do not natively support several PCRE
features, but you can emulate some. I have recently finished emulating
support for Unicode character property escape sequences, named subpatterns,
and the `x' modifier:
<http://pointedears.de/websvn/filedetails.php?repname=JSX&path=%2Ftrunk%2Fregexp.js>
> Or, is it just something flawed in my logic that made it not work as
> expected?
Both your logic and your question are flawed.
<http://jibbering.com/faq/#posting>
<http://catb.org/~esr/faqs/smart-questions.html#beprecise>
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
[toc] | [prev] | [next] | [standalone]
| From | Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> |
|---|---|
| Date | 2011-12-17 18:31 +0100 |
| Message-ID | <cckpe7t2ntjluiaqvbpotsktgq7kr74ga5@4ax.com> |
| In reply to | #9348 |
On Sat, 17 Dec 2011 02:42:04 -0800 (PST), Jason C wrote: >I have this regex, which works as expected: > >a = a.replace(/ itxtHarvested="(.*?)"/i, ""); > >I modified it to this: > >a = a.replace(/\s*itxtHarvested="*(.*?)"*/i, ""); The inner parentheses serve no purpose. I would remove them. If, however, you are actually trying to match parentheses, then you have to escape them: \( \) Hans-Georg
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web