P
P
partyzanx2020-02-25 04:33:45
JavaScript
partyzanx, 2020-02-25 04:33:45

How to replace character with link in regexp?

There is a line 'Counting words: 隻|只 zhī 猫|母 māo'
where 隻,猫 = old version of the character
and 只,母 - modern

version Should remove the traditional versions of "隻|" "猫|"
The result will be the string 'Counting words: 只 zhī 母 māo'

And from the remaining hieroglyphs to make links, it will turn out

'Счётные слова: <a href='http://localhost:4000/search?q=只'>只</a> zhī <a href='http://localhost:4000/search?q=母'>母</a> māo'


I tried to find /.\|./g and replace it with links, but I didn't figure out how to remember the last character and insert it into the link

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Somewhere Intech, 2020-02-25
@partyzanx

If you wrap in brackets (i.e. remember in this case) the second character, then in the second argument of the replace function it can be displayed through $ 1 (the count here starts from one)

phrase.replace(/.\|(.)/g, '<a href="http://localhost:4000/search?q=$1">$1</a>')

V
Vladimir, 2020-02-25
@HistoryART

const str = 'Счётные слова: 隻|只 zhī 猫|母 māo';
str.replace('隻,猫', '只,母');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question