Ryan Clark
Ryan Clark

Reputation: 79

Python - Escape Quote in Regex within input tag

I have an input tag within my python code the looks like this:

<input class="form-control input-lg" type="text" name="name" value="value" placeholder="placeholder" required pattern="^[a-zA-Z0-9.@#$()+! \'_-]+$">

I was able to escape the single quote ' in the required pattern ' but anytime I try to add a double quote ", then it closes the previous double quote (pattern="). I am looking to achieve something like:

pattern="^[a-zA-Z0-9.@#$()+! \'\"_-]+$">

where the \" should allow me to escape the quote but clearly that doesn't work like the \' works. Any help would be greatly appreciated.

Upvotes: 3

Views: 249

Answers (3)

Rohan Amrute
Rohan Amrute

Reputation: 774

You can use r in front of your pattern:

    pattern = r"^[a-zA-Z0-9.@#$()+! \'\"_-]+$"

Upvotes: 0

eph
eph

Reputation: 2028

You can use \x22 for " and \x27 for '

Upvotes: 1

vks
vks

Reputation: 67988

pattern="""^[a-zA-Z0-9.@#$()+! \'\"_-]+$"""

You can use """ instead.

Upvotes: 2

Related Questions