我尝试通过正则表达式将单引号更改为文本中的双引号。(单字)例子:,我要走了。你要飞到行星'Ziqtos‘=>我需要保留单引号的I’s,并改为双份在您的苍蝇"Ziqtos"请帮助。这是我的密码。
var myStr = "I, I can't deny I'm paralysed from the 'inside' Everyday I wake to feel the same And 'every' time you 'ask' me how I'm feeling I just smile and tell you 'that' I'm fine I, I don't know why I'm terrified of everything Just to call the doctor seems daunting For most of my life I felt a sharp uncertainty Now its just become a part of me I, I can't deny I'm paralysed from the inside Everyday I wake to feel the 'same' And every time you ask me how I'm feeling I just smile and tell you that I'm fine It's hard to stay true, to myself and to you I can't measure up to this girl you thought you knew This aching in my heart is tearing me apart But, darling all your love is somehow not 'enough' This aching in my heart is tearing me apart But, darling all your love is somehow not"
var newStr = "";
for (var i = 0; i < myStr.length; i++) {
var lastIndex = myStr.search(/\s'[a-zA-Z]/ || /[a-zA-Z]'\s/);
newStr += myStr.substr(0, lastIndex + 1);
newStr += '\"';
myStr = myStr.substr(lastIndex + 2);
}
var Phar = document.createElement("p");
Phar.innerHTML = newStr;
document.body.appendChild(Phar);发布于 2017-11-23 21:19:12
代码
\B'|'\B或者,您可以使用\B'(\w+)'\B并用"$1"替换。
用法
const regex = /\B'|'\B/g;
const str = `I'm go. You gona fly to planet 'Ziqtos'`;
const str2 = `I, I can't deny I'm paralysed from the 'inside' Everyday I wake to feel the same And 'every' time you 'ask' me how I'm feeling I just smile and tell you 'that' I'm fine I, I don't know why I'm terrified of everything Just to call the doctor seems daunting For most of my life I felt a sharp uncertainty Now its just become a part of me I, I can't deny I'm paralysed from the inside Everyday I wake to feel the 'same' And every time you ask me how I'm feeling I just smile and tell you that I'm fine It's hard to stay true, to myself and to you I can't measure up to this girl you thought you knew This aching in my heart is tearing me apart But, darling all your love is somehow not 'enough' This aching in my heart is tearing me apart But, darling all your love is somehow not`;
const subst = `"`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
const result2 = str2.replace(regex, subst);
console.log(result);
console.log(result2);
结果
输入
I'm go. You gona fly to planet 'Ziqtos'输出
I'm go. You gona fly to planet "Ziqtos"解释
\B断言\b不匹配的位置'与撇号字符匹配发布于 2017-11-23 21:23:28
在您的场景中,您可以使用这个正则表达式/\'(\w+)\'/g。
var myStr = "I, I can't deny I'm paralysed from the 'inside' Everyday I wake to feel the same And 'every' time you 'ask' me how I'm feeling I just smile and tell you 'that' I'm fine I, I don't know why I'm terrified of everything Just to call the doctor seems daunting For most of my life I felt a sharp uncertainty Now its just become a part of me I, I can't deny I'm paralysed from the inside Everyday I wake to feel the 'same' And every time you ask me how I'm feeling I just smile and tell you that I'm fine It's hard to stay true, to myself and to you I can't measure up to this girl you thought you knew This aching in my heart is tearing me apart But, darling all your love is somehow not 'enough' This aching in my heart is tearing me apart But, darling all your love is somehow not"
var txt = myStr.replace(/\'(\w+)\'/g, (_,m) =>
'"' + m + '"'
)
document.querySelector('div').textContent= txt<div>
只要您想将引号更改为单个单词,这将起作用。
https://stackoverflow.com/questions/47463356
复制相似问题