由不同regex匹配分隔的連接結果

[英]join result that is separated by a diffrent regex match


hello im doing something like how to replace dots inside quote in sentence with regex

你好,我正在做一些事情,比如如何用正則表達式替換句子中引用的點

var string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. "Vestibulum interdum dolor nec sapien blandit a suscipit arcu fermentum. Nullam lacinia ipsum vitae enim consequat iaculis quis in augue. Phasellus fermentum congue blandit. Donec laoreet, ipsum et vestibulum vulputate, risus augue commodo nisi, vel hendrerit sem justo sed mauris." Phasellus ut nunc neque, id varius nunc. In enim lectus, blandit et dictum at, molestie in nunc. Vivamus eu ligula sed augue pretium tincidunt sit amet ac nisl. "Morbi eu elit diam, sed tristique nunc."';

// seperate the quotes
var quotes = string.match(/"(.)+?"/g);
var test = [];

// for each quotes
for (var i = quotes.length - 1; i >= 0; i--) {

    // replace all the dot inside the quote
    test[i] = quotes[i].replace(/\./g, '[dot]');
};

console.log(test);

lets say we already make the change with the regex, but im stuck at how can we join it back to the existing var string as my result is seperated in var test ? or theres a better way?

假設我們已經對regex進行了更改,但是我仍然糾結於如何將其連接回現有的var字符串,因為我的結果在var測試中被分離了。還是有更好的方法?

the output should be something like

輸出應該是這樣的

Lorem ipsum dolor sit amet, consectetur adipiscing elit. "Vestibulum interdum dolor nec sapien blandit a suscipit arcu fermentum[dot]Nullam lacinia ipsum vitae enim consequat iaculis quis in augue[dot] Phasellus fermentum congue blandit[dot] Donec laoreet, ipsum et vestibulum vulputate, risus augue commodo nisi, vel hendrerit sem justo sed mauris[dot]" Phasellus ut nunc neque, id varius nunc. In enim lectus, blandit et dictum at, molestie in nunc. Vivamus eu ligula sed augue pretium tincidunt sit amet ac nisl. "Morbi eu elit diam, sed tristique nunc[dot]"

主阿,我願多洛坐在那里,與你永別。“前庭間的陰道炎,是一種叫(點)的,有刺的、有刺的、有刺的、有刺的、有刺的、有刺的、有刺的、有刺的、有刺的、有毛的、有毛的、有毛的、有毛的、有毛的、有毛的、有毛的、有毛的、有毛的。”在埃尼姆·勒克圖斯,布蘭蒂特和格言在,莫雷斯蒂在尼姑庵。Vivamus eu ligula sed augue pretium tincidunt sit amet ac nisl。"Morbi eu elit diam, sed tristique nunc[dot]"

*ps im not sure the title is corrent

*我不確定標題是corrent

thanks

謝謝

2 个解决方案

#1


2  

You could instead just split at ", do the replacement in every second array element, then join again:

你可以在" ",做替換在每個第二數組元素,然后再加入:

var parts = string.split('"');
for (var i = 1; i < parts.length; i += 2) {
    parts[i] = parts [i].replace(/\./g, '[dot]');
};
string = parts.join('"');

Since split will create an empty string at index 0 if the string starts with " this should work in all cases.

由於split將在索引0處創建一個空字符串,如果該字符串以“在所有情況下都應該有效”開頭。

Note that the edge case of a trailing unmatched " will lead to every dot after that " to be replaced as well. If you do not want this, simply change the for condition to i < parts.length - 1.

注意,尾不匹配的邊格將導致后面的每個點也被替換。如果您不想這樣做,只需將for條件更改為i < parts。長度- 1。

JSFiddle Demo

JSFiddle演示

#2


1  

Use regexp but with replace function:

使用regexp但使用替換功能:

string.replace(/"[^"]+"/g, function(m) {return m.replace(/\./g,"[dot]")})

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/10/30/72a2047d3ce1df3335000d96adf503c2.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com