I have a string like this.
var a="1:2:3:";
I want to split it with a.split(":")
to remove the ":" colon character.
I want to get this as the result:
["1","2","3"]
But instead the result of a.split(":")
is this:
["1","2","3",""]
Use this trim method to remove the trailing colon.
function TrimColon(text)
{
return text.toString().replace(/^(.*?):*$/, '$1');
}
Then you can call it like this:
TrimColon(a).split(":")
If you wanted to you could of course make TrimColon
a string prototype method, allowing you to do something like this:
a.TrimColon().split(":");
In case you'd like an explanation of the regex used: Go here
Before parsing such string you should strip colons from the beginning and the end of the string:
a.replace(/(^:)|(:$)/g, '').split(":")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With