Skip to content

解决针对''中含有的相同转义引号(即\')的错误识别,导致的包裹错误问题(双引号同理) #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gky918
Copy link

@gky918 gky918 commented Dec 8, 2024

更改正则表达式 i18nStrReg :

1、将原代码中的两部分[^'{}\n]* ,更改为(?:[^'{}\n]|\.),解决了词条开头、中间遇到相同转义引号'时,会与'中的引号错误匹配产生词条包裹错误的问题(双引号同理),但此时并不适用于结尾为'的词条(因为会与结尾的'中的\进行匹配,而将'中的'作为词条结束的引号,见Eg2)
Eg1: 词条:''一'、'二'、三'
原始表达式/'([^'{}\n]
[^\x00-\xff]+[^'{}\n])'/g : 匹配结果是 '一' 和 '二'
第一步修改后表达式/'((?:[^'{}\n]|\.)
[^\x00-\xff]+(?:[^'{}\n]|\.))'/g : 匹配结果为 ''一'、'二'、三'
Eg2:词条:''一'、'二'、'三''
原始表达式/'([^'{}\n]
[^\x00-\xff]+[^'{}\n])'/g : 匹配结果是 '一' 和 '二'和'三'
第一步修改后表达式/'((?:[^'{}\n]|\.)
[^\x00-\xff]+(?:[^'{}\n]|\.)*)'/g : 匹配结果为 ''一'、'二'、'三'(最后的'未匹配)

2、为了能够在第一步的基础上解决不适用于结尾为'的词条,将第二部分的(?:[^'{}\n]|\.)改为(?:[^'{}\n\\]|\.),使表达式不与结尾的'中的\进行匹配,不会将'中的'作为词条结束的引号(双引号同理)
Eg3: 词条:''一'、'二'、'三''
原始表达式/'([^'{}\n][^\x00-\xff]+[^'{}\n])'/g : 匹配结果是 '一' 和 '二'和'三'
第一步修改后表达式/'((?:[^'{}\n]|\.)[^\x00-\xff]+(?:[^'{}\n]|\.))'/g : 匹配结果为 ''一'、'二'、'三'
最终表达式/'((?:[^'{}\n]|\.)[^\x00-\xff]+(?:[^'{}\n\\]|\.))'/g :匹配结果是 '一' 和 '二'和'三''

以上双引号均与单引号同理,故最终修改为:
const i18nStrReg = /"((?:[^"{}\n]|\.)[^\x00-\xff]+(?:[^"{}\n\\]|\.))"|'((?:[^'{}\n]|\.)[^\x00-\xff]+(?:[^'{}\n\\]|\.))'/g

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant