简体   繁体   English

通过正则表达式将单引号更改为双引号。 (单字)

[英]Change single quotes to double quotes by Regular expression. (in single words)

I tring to change single quotes to double quotes in text by Regular expression. 我试图通过正则表达式将文本中的单引号更改为双引号。 (in single words) EXAMPLE: I'm go. (一个字) 示例:我走了。 You gona fly to planet 'Ziqtos' => I need to keep single quotes in I'm , and change to double in You gona fly "Ziqtos" Please help. 您戈纳飞到地球上Ziqtos'=>我需要保持单引号 ,并更改您仔细戈纳飞“Ziqtos”请帮助。 There is my code. 有我的代码。

    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);

Code

See regex in use here 查看正则表达式在这里使用

\B'|'\B

Alternatively, you can use \\B'(\\w+)'\\B and replace with "$1" 或者,您可以使用\\B'(\\w+)'\\B并替换为"$1"

Usage 用法

 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); 


Results 结果

Input 输入项

I'm go. You gona fly to planet 'Ziqtos'

Output 输出量

I'm go. You gona fly to planet "Ziqtos"

Explanation 说明

  • \\B Assert position where \\b does not match \\B \\b不匹配的位置
  • ' Match the apostrophe character literally '从字面上匹配撇号字符

in your scenario you can get away with this regex /\\'(\\w+)\\'/g : 在您的方案中,您可以使用以下正则表达式/\\'(\\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> 

this will work as long as you want to change the quotes to single words. 只要您想将引号更改为单个单词,此方法就起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM