userMod2
userMod2

Reputation: 9000

JS and Regex - Removing comma before a bracket

Sorry if sounds really trivial but:

I have the following line

var myString = 
    linear-gradient(to right, #f25f5c 0%,  #f25f5c 100%, #84dcc6 100%,  #84dcc6 100%, )

I want to remove the , at before that closing bracket if ever it exists.

I've used http://regexr.com/ to determine that my regex needs to be ,.\) however how do i use this as JS?

Is this correct:

if (,.\.test(myString)) {
   //how do i remove that comma?
}

Alternatively if this is a really long winded way and someone know of something simplier in JS - would appreciate it.

Thanks

Upvotes: 0

Views: 617

Answers (1)

Dalorzo
Dalorzo

Reputation: 20024

How about:

   var result= "linear-gradient(to right, #f25f5c 0%,  #f25f5c 100%, #84dcc6 100%,  #84dcc6 100%, )"
     .replace(/,\s*\)$/,')')

The regular expression is /,\s*)\)$/ where:

  • $ at the end of your string
  • ) you will look for the parenthesis
  • \s* where the space is optional
  • , between your comma and parenthesis

Upvotes: 2

Related Questions