Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.2k views
in Technique[技术] by (71.8m points)

正则匹配问题

我想从 {t('word')} {t("word1")} {t(`word2`)} {t(word3)} 捕获出这个数组 ['word', 'word1', 'word2', 'word3']

我的正则表达式是这个 /(?<=t(([`|'|"]?))(.*?)(?=1))/g.

但是,我捕获的数组里都像这样"'word'"带有引号。

所以我应该怎么改进能达到对应的效果呢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

按照你的描述,单词里的引号和包裹单词的引号重复了,那这里用正则的话就不太好搞了,只能做个取舍,比如下面这个,舍弃单词两端的所有引号,无论这个引号是不是单词本身所具备的

`{t('word"')} {t('word')} {t('that's xx')} {t("word1")} {t(`word2`)} {t(word3)}`.match(/(?<={t([`'"]*?)[^`'"][^)]*?(?=[`'"]*?)})/g)
//["word", "word", "that's xx", "word1", "word2", "word3"]

这种复杂的还是建议js或者正则加js来处理,单纯的正则过于复杂的话可能会影响性能,而且像上面出现的?<=?=兼容性都需要注意


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...