D
D
DeeZ2021-07-28 18:17:50
JavaScript
DeeZ, 2021-07-28 18:17:50

How to replace all minuses with underscores in a tag?

Good afternoon.

There is text in which you want to replace minuses with underscores. but only in those words that begin with a lattice. The number of minuses is not constant.
I assume that it should be a regular season with a look back, and there is a match, but how can I replace it?

Tell me how to do it? Perhaps the regular season is from a cannon on sparrows, and should it be done in a completely different way?

var msg = `foo-bar-bar (ssh://foo-bar-bar)
#PROBLEM #foo-bar-bar

foo-bar (ssh://foo-bar)
#PROBLEM #foo-bar 
`

const regex = /(#[\w]*)(-(\w*)){1,}/ig;

console.log(msg.replace(regex, "\$1_\$3"));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2021-07-28
@DeeZ

msg.replace(/#[\w-]+/g, (m) => m.replace(/-/g, '_'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question