XML Namespace Declaration
Match XML namespace declarations (`xmlns="..."` and `xmlns:prefix="..."`), capturing the prefix and URI.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("xmlns(?::([\\w\\-]+))?\\s*=\\s*[\"']([^\"']+)[\"']", "g");
const input = "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"xmlns(?::([\\w\\-]+))?\\s*=\\s*[\"']([^\"']+)[\"']")
input_text = "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`xmlns(?::([\w\-]+))?\s*=\s*["']([^"']+)["']`)
input := `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
xmlns(?::([\w\-]+))?\s*=\s*["']([^"']+)["'] (flags: g)Raw source: xmlns(?::([\w\-]+))?\s*=\s*["']([^"']+)["']
How it works
Examples
Input
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">Matches
xmlns="http://www.w3.org/2000/svg"xmlns:xlink="http://www.w3.org/1999/xlink"
Input
<root xmlns='urn:custom'>Matches
xmlns='urn:custom'
Input
<plain>No match
—Common use cases
- •XML schema migration tooling
- •SVG / SOAP message parsing
- •Namespace conflict detection
- •XML-to-JSON conversion preprocessors
Related patterns
XML Processing Instruction
WebMatch XML processing instructions like `<?xml version="1.0" encoding="utf-8"?>` or `<?xml-stylesheet href="..."?>`.
CSS hsl() / hsla() Color
WebMatch CSS hsl() and hsla() color functions, capturing hue (0–360), saturation, lightness, and optional alpha.
HTTP Content-Type Header
WebParse the value of an HTTP Content-Type header, capturing the media type and optional charset.
Twitter / X URL
WebMatch Twitter/X profile and status URLs, capturing the handle and (optional) tweet ID.