2

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.

1

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

0

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

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

Comments

0

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)

1 Comment

I already accepted another answer that got the job done well. thanks a lot though. learned a lot from this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.