简体   繁体   中英

Replace and transform text using regex

I have a text with a pattern. And it needs to be transformed to a text that is more readable to the user.

//Rawtext => transformed text
@[John](john@gmail.com), how are you? => @John, how are you?
Good morning @[Doe](doe@gmail.com) => Good morning @Doe.

Any help is deeply appreciated. Thanks in advance.

Note: related to the react-mentions package for reactjs

This might work.

 const regex = /@\\[([^\\]]+)\\]\\([^\\)]+\\)/g; console.log('@[John](john@gmail.com), how are you?'.replace(regex, '@$1')); console.log('Good morning @[Doe](doe@gmail.com)'.replace(regex, '@$1'));

Here is one way to do it:

 const re = /\\[(\\w+)\\]|\\(.+\\)/g const a = '@[John](john@gmail.com), how are you?' const b = 'Good morning @[Doe](doe@gmail.com)' const result_A = a.replace(re, '$1') const result_B = b.replace(re, '$1') console.log(result_A) console.log(result_B)

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