How to trim multiple characters?

  • A+
Category:Languages

I have a string as follows

const example = ' ( some string ()() here )   '; 

If I trim the string with

example.trim() 

it will give me the output: ( some string ()() here )

But I want the output some string ()() here. How to achieve that?

const example = ' ( some string ()() here )   '; console.log(example.trim());

 


You can use a regex for leading and trailing space/brackets:

function grabText(str) {    return str.replace(/^/s+/(/s+(.*)/s+/)/s+$/g,"$1"); }  console.log(   grabText('  ( some (string) here )   ') ) console.log(   grabText(' ( some string ()() here )   ') )  

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: