简体   繁体   中英

Remove ’ on string using regex

I have a word like this What's On . How to remove space and ' ?

I can remove space like so data.caption.replace(/ +/g, "") How to do the other part?

You can use [] to provide a character set. So in this case, the following would match against the weird quote and a space.

/[’ ]+/g

This expresion might simply work:

\s*’

which checks for 0 or more spaces prior to ' .

 console.log("What 's On ?".replace(/\\s*'/,"")); console.log("What 's On ?".replace(/[\\s']+/,"")); 

Or if we wish to replace all spaces:

 const regex = /([^\\s']+)|(.+?)/gm; const str = `What 's On ?`; const subst = `$1`; // The substituted value will be contained in the result variable const result = str.replace(regex, subst); console.log(result); 

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM