What is the appropriate regex string to match a specific html element? - html

I have a ton of text replacements to make and I would like to try and do this all at once instead of manually. I'm trying to replace <a class='stuff morestuff' href='#'>Some Text</a> with Some Text; essentially stripping off the surrounding anchor tag.
I've been messing around with a search/replace in Visual Studio using regex, but am not really getting anywhere. My latest attempt:
Find what:
\<a class='stuff morestuff' href='#'\>(.+)\<\/a\>
Replace with:
$1
If what I want to do is even feasible, how can I correct my regex to accomplish this?

This regex will match your anchors if the class and href are always the same:
Find: \<a[^\>]class='stuff morestuff' href='\#'[^\>]*\>(.*)\</a\>
Replace: $1
This regex will replace all the anchors with the inner text:
Find: \<a[^\>]*\>(.*)\</a\>
Replace: $1
I'm assuming from your post you plan to use this in Visual Studio's Find/Replace and not in code.

Find:\\<a class='.*?' href='#'>(.*?)\\</a\\>
Replace: $1

Related

IntelliJ replace text with tag

I need to replace some html file names in my html file with anchor tags, such that:
abc.html
file.html
becomes:
abc.html
file.html
I have though of the following regex in find: [a-z-0-9]+\.html and it seems to work, but I don't know what to add in replace. I have tried some answers from here , but they did not work. Can you please help?
You need to use capturing groups, that then you can refer back to in the replace box..
Capture groups are created adding parenthesis to find: ([a-z-0-9]+\.html)
Then replace would be: $1
$1 means the first capture group.

How to wrap links in <a> with Notepad++ Find/Replace function?

I have a text document with raw links (not wrapped) and I would like to wrap them in HTML anchor tags.
Link example:
http://example.com/images/my-image.jpg
Desired output:
http://example.com/images/my-image.jpg
I can FIND the links in Notepad++ using the following RegEx:
[-a-zA-Z0-9#:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9#:%_\+.~#?&//=]*)?(\?([-a-zA-Z0-9#:%_\+.~#?&//=]+)|)
However, the REPLACE string I'm trying is not working for some reason:
\1
How can I do this with notepad++?
You need to replace with the backreference to the whole match:
$&
Or
$0
Here, the $0 and $& "insert" the text that is matched by the whole regular expression, not just by some capturing groups.

Alternative to lookbehind with variable width

I have some html which contains a number of hyperlinks to html files, but they don't have any file extensions.
For example in the string <a href='variablelengthfilename'> I'm trying to match the trailing ' , so I can replace it with .html' (using a RegEx search in Notepad++) using something like this:
`(?<=href='[A-Za-z]*)'`
but that won't work because Notepad++ doesn't allow variable-length lookbehind assertions.
How else can I achieve this?
Thanks
Since you are working in Notepad++, here is a way to achieve what you are after:
Find what: \bhref='[^']*
Replace with: $&.html
The \bhref='[^']* regex matches a href as a whole word, then =' are matched literally, and [^']* matches 0 or more characters other than '. Note you will need to replace ' with " if the href value is inside double quotes.
Assuming all your links look like that, why not just do a simple replace
'>
with
.html'>
?

Regex that matches any HTML tag with the content inside

I'd like to use Regex to match HTML tag "head" and text inside them so I can delete them easily. I'm using a find and replace tool that is utilizing regex syntax and it really works great in replacing multiple files at once.
I tried doing a lot of syntax but I always fail.
http://regex101.com/r/aZ6pN5/2
Anyone can help please?
Replace .* in your regex with [\S\s]*?, so that it would match line breaks also. You can't use s DOTALL modifier in JavaScript.
<head.*?>([\s\S]*?)<\/head>
[\s\S]*? This would do an non-greedy match of zero or more space or non-space characters.
DEMO
OR
To replace the contents of head tag.
(<head\b[^<>]*>)[\s\S]*?(<\/head>)
Replacement string:
$1stringyouwant$2
DEMO

How to replace a specific line of HTML code with Regular Expression In Dreamweaver?

I want to replace <whatever>Some Title</whatever> with <something>Some Title</something> using the Find and Replace tool inside of Dreamweaver. How do I perform?
Not a Dreamweaver user, but this simple approach works in my editor (Emacs):
Replace:
<whatever>\(.*\)</whatever>
With:
<something>\1</something>
This is a pretty straightforward approach but it may fall short of your needs. Do some or all of your <whatever> element pairs occupy more than one line of text? Or do you have more than one <whatever> pair on a single line?
i guess what you want is to change all your <whatever> tag with an <something> tag whitout changing your text, right?
If it is so, you want to use find and replace with regular expression. Find (in source code) <whatever>(.*)</whatever> and replace it with <something>$1</something>. The $1 is used as a variable for anything fits the (.*) part DW finds for each instance.
For example, you you want to comment all instances of an
document.NAMEOFANYFORMONTHEPAGE.WHATEVERNAME.focus();
in a JavaScript file, you would use find:
document\.(.*)\.focus\(\);
and replace it with:
// document.$1.focus();
Don't forget to escape special characters and, please, try a few instances before using Replace All