Javascript function to remove leading point

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?

+3
source share
4 answers

The following replaces the dot at the beginning of the line with an empty line, leaving the rest of the line untouched:

a.replace(/^\./, "")
+7
source

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.


Resources

+7
source

.

var a = '.2.98»';
document.write(a.replace('/^\.(.+)/',"$1"));

(.+) , , \..

0

:

if (a.charAt(0)=='.') {
  document.write(a.substr(1));
} else {
  document.write(a);
}
0
source

All Articles