将单引号更改为正则表达式的双引号。 (用一句话说)

时间:2017-11-23 21:16:31

标签: javascript regex

我想通过正则表达式将单引号更改为文本中的双引号。 (用一句话说) 示例:我走了。你可以飞到'Ziqtos'行星 => 我需要在中保留单引号,然后在你的gona fly “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);

2 个答案:

答案 0 :(得分:4)

代码

See regex in use here

\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不匹配的位置
  • '按字面意思匹配撇号字符

答案 1 :(得分:1)

在您的方案中,您可以使用此正则表达式/\'(\w+)\'/g

&#13;
&#13;
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
&#13;
<div>
&#13;
&#13;
&#13;

只要您想将引号更改为单个单词,

就会有效。