簡體   English   中英

通過正則表達式將單引號更改為雙引號。 (單字)

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

我試圖通過正則表達式將文本中的單引號更改為雙引號。 (一個字) 示例:我走了。 您戈納飛到地球上Ziqtos'=>我需要保持單引號 ,並更改您仔細戈納飛“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);

查看正則表達式在這里使用

\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不匹配的位置
  • '從字面上匹配撇號字符

在您的方案中,您可以使用以下正則表達式/\\'(\\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> 

只要您想將引號更改為單個單詞,此方法就起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM