javascript - Regular expression to remove more than one continuous "--" -
i have 2 requirements, first, want replace "-" symbol in both beginning , end of text empty value. second, if there continuous "-" symbols should replaced single "-" symbol.
if possible please provide code both requirements in single pattern.
code:
//1.) // replace more 1 "-" in b // expected output : -asdas-sadf-asdasd-ju var = "--asdas-sadf----asdasd---ju"; = a.replace(/-{2,}/,""); //alert(a); //2.) // remove last "-" , starting "-" b "das-" - after das needs removed // expected output : welcome/asasdgrd/asd-ast-yret-das/456 var b = "-welcome/asasdgrd/asd-ast-yret-das-/456" b = b.replace(/[-$]/,""); //alert(b);
fiddler link:
you need use capturing groups.
var s = "--asdas-sadf----asdasd---ju"; alert(s.replace(/^-+|-+$|(-)+/gm, "$1"));
Comments
Post a Comment