简体   繁体   English

使用Javascript中的正则表达式替换大文本中两个字符之间的每个出现字符串

[英]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.

相关问题 正则表达式以提取两组字符之间的文本(Javascript) - Regular expression to extract text between two sets of characters (Javascript) 使用Javascript中的正则表达式将最后一次出现的数字替换为* - Replace last occurrence of number with * using regular expression in Javascript 如何在JavaScript中的字符串的最后两个字符之间替换一个字符? - How do I replace a character between the last occurrence of two characters in a string in JavaScript? JavaScript正则表达式替换字符串中的转义特殊字符 - JavaScript Regular Expression to Replace Escape Special Characters from String 如何在JavaScript中使用正则表达式用空字符串替换一组字符? - How to use Regular expression in JavaScript to replace a set of characters with an empty string? 如何使用javascript替换字符串中字符的最后一次出现 - How to replace last occurrence of characters in a string using javascript Javascript替换两个字符之间的字符串中间 - Javascript replace middle of string between two characters JavaScript中的正则表达式如何在两个“特殊”字符之间查找内容? - Regular expression in javascript how to find content between two “special” characters? 在JavaScript中执行正则表达式以匹配两个字符 - Doing a regular expression in JavaScript to match between two characters 使用xregexp的javascript正则表达式替换特殊字符,但允许白名单 - javascript regular expression to replace special characters, but allow a whitelist, using xregexp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM