Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #9349
| Date | 2011-12-17 14:08 +0100 |
|---|---|
| From | Stefan Weiss <krewecherl@gmail.com> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: String.replace() with regex |
| References | <16483917.24.1324118524679.JavaMail.geo-discussion-forums@yqlp16> |
| Message-ID | <LsadnYc-WN0gCXHTnZ2dnUVZ8vCdnZ2d@giganews.com> (permalink) |
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
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web