I have a javascript string that has a leading point. I want to remove the leading point using the javascript replace function. I tried the following code.
var a = '.2.98»'; document.write(a.replace('/^(\.+)(.+)/',"$2"));
But that does not work. Any idea?
The following replaces the dot at the beginning of the line with an empty line, leaving the rest of the line untouched:
a.replace(/^\./, "")
Do not perform regular expressions if you do not need it.
Simple charAt()and a substring()or substr()(only if charAt(0)is .) will suffice.
charAt()
substring()
substr()
charAt(0)
.
Resources
var a = '.2.98»'; document.write(a.replace('/^\.(.+)/',"$1"));
(.+) , , \..
(.+)
\.
:
if (a.charAt(0)=='.') { document.write(a.substr(1)); } else { document.write(a); }