sublimetext2 - Replacing an open-ended HTML regex match in Sublime Text -
i've been doing lot of finding , replacing in sublime text , decided needed learn regex. far, good. i'm no expert means, i'm learning quickly.
the trouble knowing how replace open-ended html matches.
for example, wanted find <button>s didn't have role attribute. after hacking , searching, came following pattern (see in action):
<button(?![^>]+role).*?>(.*?)
great! except, in code base i'm working in, there tons of results.
how do replacing results safely injecting role=button @ end of <button, before closing > in opening tag?
desired results
before: <button type="button">
after: <button type="button" role="button">
before: <button class="btn-lg" type="button">
after: <button class="btn-lg" type="button" role="button">
you can capture before ending > , put back, before insertion of role=button:
<(button(?![^>]+role).*?)> this captures in tag.
replace by:
<$1 role="button"> the $1 contains first regex captured.
see updated regexr.
Comments
Post a Comment