首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过正则表达式将单引号更改为双引号。(单字)

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

Stack Overflow用户
提问于 2017-11-23 21:16:30
回答 2查看 39关注 0票数 1

我尝试通过正则表达式将单引号更改为文本中的双引号。(单字)例子:,我要走了。你要飞到行星'Ziqtos‘=>我需要保留单引号的I’s,并改为双份在您的苍蝇"Ziqtos"请帮助。这是我的密码。

代码语言:javascript
复制
    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);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-23 21:19:12

代码

参见此处使用的regex

代码语言:javascript
复制
\B'|'\B

或者,您可以使用\B'(\w+)'\B并用"$1"替换。

用法

代码语言:javascript
复制
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);

结果

输入

代码语言:javascript
复制
I'm go. You gona fly to planet 'Ziqtos'

输出

代码语言:javascript
复制
I'm go. You gona fly to planet "Ziqtos"

解释

  • \B断言\b不匹配的位置
  • '与撇号字符匹配
票数 4
EN

Stack Overflow用户

发布于 2017-11-23 21:23:28

在您的场景中,您可以使用这个正则表达式/\'(\w+)\'/g

代码语言:javascript
复制
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
代码语言:javascript
复制
<div>

只要您想将引号更改为单个单词,这将起作用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47463356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档