[英]Replace every occurrence string between two characters in a large text using regular expression in Javascript
Hi We have the following text 嗨我们有以下内容
This is fruit is $apple$ and second fruit is $mango$ and the third fruit is $banana$ 这个水果是$ apple $ ,第二个水果是$ mango $ ,第三个水果是$ banana $
Now in JavaScript we need to find text between $..$ and replace with my own text on every occurrence randomly 现在,在JavaScript中,我们需要在$ .. $之间查找文本,并在每次出现的情况下随机替换为我自己的文本
This is fruit is $five apples$ and second fruit is $two mangoes$ and the third fruit is $bananas are yellow$ 这是水果,是五个苹果$ ,第二个水果是两个芒果$ ,第三个水果是$香蕉,黄色
some thing like this, the main objective is find all strings between $$ and replace with new text. 像这样的事情,主要目标是找到$$之间的所有字符串并替换为新文本。
can any one help me simple java script regular expression or any other method which is fast. 谁能帮助我简单的Java脚本正则表达式或其他快速的方法。
I think what you want is this: 我认为您想要的是:
var text = "This is fruit is $apple$ and second fruit is $the mangoes$ and the third fruit is $ price \\$ of banana$";
var replace = ['hello', 'these', 'are', 'some', 'random', 'strings'];
var matches = text.match(/\$(?:[^\$\\]|\\.)*\$/g);
matches.forEach(function(match) {
random = '$' + replace[Math.floor(Math.random() * replace.length)] + '$';
text = text.replace(match, random)
});
alert(text);
Something like this should work: 这样的事情应该起作用:
https://jsfiddle.net/mewcg3zo/ https://jsfiddle.net/mewcg3zo/
var text = "This is fruit is $apple$ and second fruit is $mango$ and the third fruit is $banana$";
var matches = text.match(/\$(.*?)\$/g);
var newText = ['$five apples$', '$two mangoes$', '$bananas are yellow$'];
$.each(matches, function(index, match) {
text = text.replace(match, newText[index]);
});
alert(text);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.