Shashank Cool
Shashank Cool

Reputation: 91

Trimming String in Javascript

I have string like var a=""abcd""efgh"".How do I print the output as abcd""efgh by removing first and last double quote of a string I used a.replace(/["]/g,'') but it is removing all the double quotes of a string.How do i get the output as abcd""efgh.Suggest me an idea.

Upvotes: 0

Views: 61

Answers (2)

shyam
shyam

Reputation: 9368

You can use

var a='"abcd""efgh"';
a.replace(/^"+|"+$/g, '');

From the comments here is the explanation

Explanation

  • There are 2 parts ^"+ and "+$ separated by | which is the regex equivalent of the or
  • ^ is for starts-with and "+ is for one or more "
  • Similarly $ is for ends-with
  • The //g is for global replacement otherwise the only the first occurrence will be replaced

Upvotes: 10

Try use this

var a='"abcd""efgh"';
a.replace(/^"|"$/g, '');

Upvotes: 0

Related Questions