---
title: "Regex - Match attribute in an HTML code"  
description: "Regex - Match attribute in an HTML code"  
author: "Revati S Misra"  
published: 2023-08-29  
updated: 2023-09-04  
canonical: https://www.mindstick.com/forum/159713/regex-match-attribute-in-an-html-code  
category: "html"  
tags: ["html", "regex", "html element"]  
reading_time: 1 minute  

---

# Regex - Match attribute in an HTML code

[Regex](https://www.mindstick.com/forum/160352/how-to-use-regex-in-javascript-for-pattern-matching) - [Match](https://answers.mindstick.com/qa/49176/who-invented-the-match-safety) [attribute](https://www.mindstick.com/blog/221/attributes-reflection) in an [HTML](https://www.mindstick.com/articles/1530/design-a-simple-stylish-calculator-using-html-css-and-javascript) [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii)

## Replies

### Reply by Aryan Kumar

Sure, here is a regular expression to match an attribute in an HTML code:

```plaintext
<\w+ \w+=\"([^\"]+)\">
```

The `<` metacharacter matches the beginning of an HTML tag. The `\w+` metacharacter matches one or more word characters. The `\w+` metacharacter matches one or more word characters. The `=\"` metacharacter matches an equal sign. The `([^\"]+)` part of the pattern matches any character except a double quote, and the `\">` metacharacter matches the end of an HTML tag.

So, this regex pattern will match any HTML tag that has an attribute with a value.

For example, the following HTML code would be matched by this regex pattern:

HTML

```plaintext
<input type="text" name="username">
```

The `type` and `name` attributes would be matched by the pattern.

The following HTML code would not be matched by this regex pattern:

HTML

```plaintext
<h1>This is a heading</h1>
```

The `h1` tag does not have any attributes.


---

Original Source: https://www.mindstick.com/forum/159713/regex-match-attribute-in-an-html-code

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
