0

I'd need a Javascript function to remove leading and trailing whitespaces between asterisks.

Eg. to transform this string:

Hello **world **how are you?

Into

Hello **world** how are you?

Also

Hello ** world** how are you?

Into the same result.

Which would be to make that markdown type string input valid.

Thanks!

Edit: I do not need a regex alone but a way to achieve this. I tried /\*\*(\S(.*?\S)?)\*\*/gm for a particular word between asterisks but it wouldn't serve purpose on an entire string.

keelhauled
  • 37
  • 4
  • How static is this format? Does it ever change or is it always exactly 2 asterisks, word, space, 2 asterisks, no space? – dvo Aug 28 '19 at 19:07
  • I added another example and more information. I'd need to remove leading and trailing whitespaces between asterisks :). – keelhauled Aug 28 '19 at 19:08
  • 2
    Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/questions/4736) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Aug 28 '19 at 19:10
  • @WiktorStribiżew I'm not exactly looking for a regex.. but for a way to achieve this text transformation. I tried with `/\*\*(\S(.*?\S)?)\*\*/gm` for a specific word but would need to get applied to every word in the string. I edited my post. – keelhauled Aug 28 '19 at 19:40
  • But `\*\*(\S(.*?\S)?)\*\*` does not attempt to match a string with trailing/initial whitespaces between double asterisks. Why not use ``\*\*([\s\S]?)\*\*`` and replace with an anonymous method to trim whitespace from Group 1? – Wiktor Stribiżew Aug 28 '19 at 20:03
  • Please update the question with the exact requirements, then it will be eligible for reopening, as now, it is not clear what you want to match exactly. – Wiktor Stribiżew Aug 28 '19 at 20:22
  • Those who cast reopen votes: why? How is the question answerable now? The requirements are still unknown. – Wiktor Stribiżew Aug 28 '19 at 21:45
  • You could try this: DEMO: https://regex101.com/r/Io5o4E/1 – Jorge Wander Santana Ureña Aug 29 '19 at 12:07

0 Answers0