I need to assign a regex object to an input element pattern attribute programmatically. Below is my current implementation:
var regex = /\d{5}/;
element.attr("pattern", regex.toString().slice(1,-1);
Is there a better way to do this without string manipulation?
regexobject for anything else? If not you can obviously just assign the string directly withelement.attr("pattern","\\d{5}").RegExpinstances have asourceproperty which contains their text, so/\d{5}/.source === "\\d{5}".\d{5}should do (and does in Firefox).