最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

replace multiple different characters with different values javascript - Stack Overflow

programmeradmin0浏览0评论

I am trying to replace different set characters with corresponding values. for example every < to be replaced with a #U34 and every $ to replaced with )#89.

I have an array of strings that have those characters randomly thrown about. for example:

var arr = [
'uisdhfu<',
'u$$fd<'
]

so far I figured out that i can do:

  var replace = /</ig;
    var newString = textWithCharacters.replace(replace, '#U34');

but this seems like it can only be done for one character at a time. and if i want to do more than one I seems like i need to create a new string each time. is there a way to do this in one go? maybe with a loop and if statements? but i can't seem to figure out how i would define the conditions of the loop.

I am trying to replace different set characters with corresponding values. for example every < to be replaced with a #U34 and every $ to replaced with )#89.

I have an array of strings that have those characters randomly thrown about. for example:

var arr = [
'uisdhfu<',
'u$$fd<'
]

so far I figured out that i can do:

  var replace = /</ig;
    var newString = textWithCharacters.replace(replace, '#U34');

but this seems like it can only be done for one character at a time. and if i want to do more than one I seems like i need to create a new string each time. is there a way to do this in one go? maybe with a loop and if statements? but i can't seem to figure out how i would define the conditions of the loop.

Share asked Sep 22, 2016 at 19:01 sourlemonaidsourlemonaid 5042 gold badges6 silver badges20 bronze badges 1
  • 1 You can match multiple different characters and use a replacer function that dynamically decides what to insert based on the match. developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Bergi Commented Sep 22, 2016 at 19:09
Add a ment  | 

4 Answers 4

Reset to default 7

the .replace() method accepts a function for the second argument that gets passed the matched string (e.g. '<'), and returns the replacement text (e.g. '#U34').

So, you can do something like this:

var replacementMap = {
  '<': '#U34',
  '$': ')#89'
}

// Create a RegExp to match any of the characters that are used as keys in the map.
// For this example, this RegExp is the same as /[<$]/gi
// Note: this method of creating a RegExp may not work with certain characters such
// as -, ], or \
var replaceRegex = new RegExp('[' + Object.keys(replacementMap).join('') + ']', 'ig');

function getReplacementString(input) {
  return replacementMap[input];
}

var newString = textWithCharacters.replace(replaceRegex, getReplacementString);

You can have a list of the characters you want to replace and then pass a function to the replace method so it replaces your match with the correct mapping. Something like this:

var arr = [
'uisdhfu<',
'u$$fd<'
];
var mapping = {
  "$":")#89",
  "<":"#U34",
};
var regex = new RegExp(Object.keys(mapping).map((key)=>"\\"+key).join("|"),"g");
var results = arr.map((string)=>string.replace(regex,(match)=>mapping[match]));
console.log(results);

You can use many characters like this : (using a regex)

.replace(/(x|y|z)/, '')

you want something like this (run snippet and look console.log)

Here the list of HTML Number : http://www.ascii.cl/htmlcodes.htm

Have fun :)

var array = ['#!toto','&#(i u%','$mpo*+'];

var symbol = [' ','!','"','#','$','%','&',"'",'(',')','*','+',',','etc...'];

console.log(array);

for(var i = 0; i < array.length ; i++){
      
      var temp = '';
      for(var j = 0; j < array[i].length ; j++){
          
          var index = symbol.indexOf(array[i][j]);

          if(index != -1){
             temp = temp + '&#'+(32+index); 
          }
          else{
              temp = temp + array[i][j];
          }
      }
      array[i] = temp;
}

console.log(array)

发布评论

评论列表(0)

  1. 暂无评论