Applying Regular Expressions in Java to "convert" a CSS style into an HTML tag style

In my Java code, I have a String variable that contains HTML code like this:

<span style="text-decoration: underline;">test</span>

My goal is to convert it to look like this:

<u>test</u>

If the HTML code is like this:

<span style="color: #2873ee; text-decoration: underline;">test</span>

I want it to be transformed into:

<font color="#2873ee"><u>test</u></font>

I am currently using regex for this purpose:

affectedString.replaceAll("<span style=\"text-decoration: underline;\">(.*?)<\\/span>", "<u>$1</u>");

and

affectedString.replaceAll("<span style=\"color:\\s*?(#[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3}); text-decoration: underline;\">(.*?)<\\/span>", "<u><font color=\"$1\">$2</u></font>");

However, there are some issues with this approach and I find it cumbersome. I have to specify each case of coincidence which is not efficient. Additionally, the regex does not work well with nested spans like in the example:

<span style="text-decoration: underline;">test <span style="text-decoration: line-through;">two</span></span>

The result becomes incorrect due to regex matching issues.

I'm seeking advice on a better solution or an improved regex pattern to handle these scenarios. Any suggestions would be appreciated. Thank you.

Answer №1

My recommendation would be to steer clear of using regex. It can be difficult to troubleshoot and expand upon, and things can quickly become messy. Instead, consider utilizing a tool like jsoup to analyze the HTML, navigate through the DOM, and employ CSS selectors to access Elements. For example, to retrieve all divs with a class attribute, you could use

Elements divs = doc.select("div[class]");

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When an image is placed inside a parent div, the link should extend to the entire width of the div, rather than just hovering over

Essentially, I have a div that spans the entire width of its container, inside it is an image link. The image's width is set to adjust automatically with a fixed height of 600px. The link covers the whole width of the div, which in this case is the wi ...

Creating circular shapes using Mapbox SDK on Android devices

In my application, I am working with a LatLng position along with a radius specified in either kilometers or miles. Could you please advise me on how I can effectively display a circle on the map using the Mapbox Android SDK? I am considering using the C ...

Establish a connection using Angular 4 HTTP POST to communicate with a Java REST API, as it is not permitted on the server

I am facing an issue with my Angular 4 client service that is calling a REST method declared on a Java server using a PostMapping annotation. When I make the call from Angular, it is not accepted by the server. However, when testing it on Postman, it work ...

What is a sneaky way to conceal CSS specifically from iPhones without affecting other browsers?

I have a solution to conceal CSS from all browsers except for iPhones. You can find the details here: How do I apply a stylesheet just to the iPhone (and not IE), without browser sniffing? Now, my question is: How can I hide CSS specifically from iPhones ...

Dynamically insert innerHTML content into table rows using JavaScript in PHP is a fantastic way to customize

Having trouble with innerHTML in this scenario, looking to achieve something along these lines: <table width="100%" border="0" id="table2"> <?php include("connection.php"); $sql=mysql_query("select* from b1"); while($res=mys ...

CSS: The calc() function does not take into account the bottom padding when used in Firefox and Internet

I am encountering an issue with the calc() function in Firefox 32 and IE 11, where the bottom padding of a div is not being respected. However, this problem does not occur in Chrome and Opera. The main content section should have a fixed height while allo ...

How can you hide specific elements in HTML/CSS based on window size without relying on media queries?

How can I create an HTML/CSS toolbar where specific elements disappear completely when the browser size is decreased, similar to how the favorites bar functions in most browsers? The toolbar should be able to display a varying number of items. In this cas ...

Selenium ElementClickInterceptedException error encountered while utilizing Selenium and Java in headless operation

I'm currently working on a project using Java and Selenium. The tests are running fine in UI mode, but I encounter an error when running them in headless mode. org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element & ...

Issue with GSON serialization resulting in incorrect JSON output

Need some help with serializing an object of this format using GSON https://i.stack.imgur.com/Luhqz.png Below is the code I am currently using: public String encode(Object object){ return this.gson.toJson(object); } The issue lies in the output tha ...

What is the best way to dynamically insert a new row into a table, with each row containing a table heading and column?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="tbl" class="tbl1"> <tr> <th> mobileno </th> <td class='mo' id="mo_0"> </td> ...

Could you provide me with some information regarding the relationship between screen resolution and screen size?

When checking my website on different screen resolutions, I rely on a tool called Screenfly from quirktools.com. While using this tool, I came across an interesting feature - the display icon in the top left corner with a dropdown menu showing resolution ...

Learn how to extract the anchor href attribute for the <div class="_6ks"> displayed in the screenshot below using Selenium with Java

What is the best way to retrieve the anchor href attribute for the image displayed in the screenshot below using Selenium with Java? https://i.sstatic.net/l41YI.png ...

Using regular expressions to identify the presence of "&", parentheses, and consecutive letters in a string

I specialize in working with JavaScript and I have a need to verify if my text contains certain characters. Specifically, I want to check for the presence of parentheses (), ampersands (&), and repeating letters within the text. Here are some examples: te ...

Utilizing pseudo elements (after) in CSS with the font family in Font Awesome 6 fails to function properly

I am attempting to create the div using CSS after pseudo-element and I have included the Unicode in the content with the font family being "Font Awesome 6 Free". However, nothing is showing up and I'm not sure why. Can someone please assist me? Here i ...

(Vue.js) The icon fails to be applied

<link rel="icon" href="/static/icons/apple-touch-icon.png" type="image/x-icon"> <link rel="icon" type="image/png" sizes="32x32" href="/static/icons/apple-touch-icon.png"> <link rel="icon" type="image/png" sizes="16x16" href="/static/ ...

Python: Printing a string that matches a regular expression

I am looking to extract a specific word from a string and display it, similar to what the grep -o command does. For instance, let's say the word I want to find is "yellow dog" within a multi-line string: [34343] | ****. "Example": <one>, yello ...

What is the best way to change the size of a background image

I recently discovered a helpful tutorial on resizing background images that I found very insightful. If you're interested, you can check it out here. After attempting to apply this method to a 900px width div in order to resize only vertically, I enc ...

How to eliminate a hyperlink from an HTML element with the help of JQuery

Recently, I was assigned to revamp a website for the company I work for. However, upon closer inspection, I realized that the website is quite messy and relies heavily on templates, resulting in certain elements being auto-generated as active links. The i ...

Learn how to retrieve URL parameters using HTML code

I would like to update the URL without refreshing the page as I am using an AJAX function to refresh a specific div. The issue I am encountering is that when the div is reloaded via AJAX, it does not recognize the URL parameter. Below is my code (I have a ...

Get notified about the number of cells you've selected by simply dragging on a table

Is there a way to display the number of selected cells in a table when dragging with the mouse? I have a table set up, and I'd like to create an alert that shows how many cells are selected while dragging. Here is the link to my fiddle: http://jsfiddl ...