Reputation: 3
I would like to write a regular expression to do some replacing in Dreamweaver. I need a regular expression for the <abbr>
tag in html.
For example, in my page the following text will appear:
Photo: NASA
I would like it to become:
Photo: <abbr title="National Aeronautics and Space Administration">NASA</abbr>
Note that some instances of the word "NASA" in my HTML is already surrounded by <abbr>
tags. If that is the case, don't put additional <abbr>
tags around it.
Upvotes: 0
Views: 652
Reputation: 31
Search for: NASA
Replace with:
<abbr title="National Aeronautics and Space Administration">NASA</abbr>
Then search for:
<abbr title="National Aeronautics and Space Administration"><abbr title="National Aeronautics and Space Administration">NASA</abbr></abbr>
Replace with:
<abbr title="National Aeronautics and Space Administration">NASA</abbr>
Tada! =)
Upvotes: 1
Reputation: 19867
I've left out lookbehinds. I'm not sure what type of regex Dreamweaver supports.
Try using the following regular expression:
NASA(?!( *</abbr>)|(\.\w+)|("))
It will not match any instance of NASA that is:
</abbr>
regardless of whitespace between NASA and </abbr>
.
and other characters, e.g. nasa.gov is considered invalid./
, e.g. test.gov/nasa/ is considered invalid."
it's invalid.The regexp will match NASA when it's in a quote like this:
"This NASA is surrounded by quotes"
but won't when it's the only thing in quotes:
"NASA"
. . . you can see the trend here, tack on additional "or" statements where necessary.
I tested it with the following string:
This sentence ends with NASA. This Regex will work with NASA, but not with URLs such as nasa.gov/test/nasa/page.html. It won't work with <abbr>NASA</abbr> as it's surrounded by <abbr> tags. "This NASA is surrounded by quotes", but will be matched as it's not formatted like this "NASA" is.
Then you can simply do a find using that regex, and replace with:
<abbr title="National Aeronautics and Space Administration">NASA</abbr>
Note: The quotes are a limitation that I can't get around, or I don't know how to get around easily. I suggest that if you have to do this for a lot of documents that you use some type of scripting language (Python for example) and avoid one large regexp and break it down into logical statements, but that is another question entirely.
Best of luck!
Upvotes: 0
Reputation: 59699
If lookbehinds are supported, this is fairly simple.
Search:
(?<!>)NASA
Replace:
<abbr title="National Aeronautics and Space Administration">NASA</abbr>
This finds NASA
entries that are not prefixed with an >
. So, NASA
will match, >NASA
will not.
Upvotes: 0