javascript - Replacing closing HTML tags with HTML tag + \n -
i'm trying replace closing html tags closing tag + line break, found similar posts here on so, none helped me accomplish i'm looking for.
</li>
</li>\n
<img property />
<img property />\n
i managed in php following funtion, works well:
public static function addlbinhtml($htmlcode){ $pattern= array('~(</.*?>)~','(/>)'); $replace= array('${1} hallo \n ','/>\n '); return preg_replace($pattern, $replace, $htmlcode); }
i'm trying same in javascript / jquery , i'm failing on getting variable(in php regex ${1}
).
i tried .split .join , .replace, , think .replace right way go.
here got (my last , closest attempt)
function setlinebreaks(taid){ var strcontent = $('#'+taid).val(); var regex = new regexp("</.*?>", "gi"); strcontent.replace(regex, "${1} \n ") .replace(/\/>/g,'/> \n '); console.log(strcontent); $('#'+taid).val(strcontent); }
thanks in advance help
you have capture regular expression brackets, assign replaced string , replace ${1}
$1
:
var regex = new regexp("(</.*?>)", "gi"); strcontent = strcontent.replace(regex, "$1 \n ") .replace(/\/>/g,'/> \n ');
or can put replace
methods val
function.
Comments
Post a Comment