Remove string characters from a string if not matched in an array

soum

I am trying to loop over an array which contains strings that I want my input string to compare with. So approach is I am looping over an input string to check if each character matches with one of the elements present in the array. If not just replace that character with just ''. Note: regular expression is really not an option.

Here is how my JavaScript looks like

var input = 'this is A [{}].-_+~`:; *6^123@#$%&*()?{}|\ ';
input.toLowerCase(input)

var allowed = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d', 'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','s','à','â','ä','è','é','ê','ë','î','ï','ô','œ','ù','û','ü','ÿ','ç','À','Â','Ä','È','É','Ê','Ë','Î','Ï','Ô','Œ','Ù','Û','Ü','Ÿ','Ç', ' '] 

var cleanStr = '';
for(var i = 0; i < input.length; i++){
    for(var j = 0; j< allowed.length; j++){
    if(input[i] !== allowed[j]){
        cleanStr = input.replace(input[i], ' ');
      console.log(cleanStr);
    }
  }
}

The console log output doesn't appear to be any different than the input field. What am I missing?

Here is my fiddle

https://jsfiddle.net/sghoush1/nvnk7r9j/4/

Tushar

You can do this in a single loop.

var input = 'this is A [{}].-_+~`:; *6^123@#$%&*()?{}|\ ';
input = input.toLowerCase(); // Note the syntax here

var allowed = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'à', 'â', 'ä', 'è', 'é', 'ê', 'ë', 'î', 'ï', 'ô', 'œ', 'ù', 'û', 'ü', 'ÿ', 'ç', 'À', 'Â', 'Ä', 'È', 'É', 'Ê', 'Ë', 'Î', 'Ï', 'Ô', 'Œ', 'Ù', 'Û', 'Ü', 'Ÿ', 'Ç', ' '];

var cleanStr = '';

// Loop over each character in the string
for (var i = 0; i<input.length; i++) {

    // Check if the character is allowed or not
    if (allowed.indexOf(input[i]) !== -1) {
        // Concat the allowed character to result string
        cleanStr += input[i];
    }
}

console.log(cleanStr);
document.body.innerHTML = cleanStr;


RegEx Approach:

You can create RegEx from a string using the RegExp constructor. To replace non-allowed characters, negated character class RegEx can be used.

var regex = new RegExp('[^' + allowed.join('') + ']', 'g');
var cleanStr = input.replace(regex, '');

Note: You'll need to escape meta-characters that have special meaning in the Character class.

Meta-characters that are needed to escape by preceding backslash \ in the character classQuoting from www.regular-expressions.info.

In most regex flavors, the only special characters or metacharacters inside a character class are the closing bracket (]), the backslash (\), the caret (^), and the hyphen (-).

var input = 'this is A [{}].-_+~`:; *6^123@#$%&*()?{}|\ ';
var allowed = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 's', 'à', 'â', 'ä', 'è', 'é', 'ê', 'ë', 'î', 'ï', 'ô', 'œ', 'ù', 'û', 'ü', 'ÿ', 'ç', 'À', 'Â', 'Ä', 'È', 'É', 'Ê', 'Ë', 'Î', 'Ï', 'Ô', 'Œ', 'Ù', 'Û', 'Ü', 'Ÿ', 'Ç', ' '];

var regex = new RegExp('[^' + allowed.join('') + ']', 'gi');
console.log(regex);

var cleanStr = input.replace(regex, '');
console.log(cleanStr);

If the allowed characters array is fixed, you can use following RegEx to replace the non-allowed characters. Also, there is no need to convert the string to lower-case, use i flag for case-insensitive match.

var regex = /[^0-9a-zàâäèéêëîïôœùûüÿç ]/gi;

RegEx101 Live Demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Remove string characters from a string if not matched in an array

From Dev

Remove matched characters from array values if it's on last position of sentance

From Dev

remove characters from a python string

From Dev

Remove characters from String in Haskell

From Dev

Remove specific characters from a string

From Dev

Remove characters from query string

From Dev

Remove characters from a string then reverse it

From Dev

Remove specific characters from a string

From Dev

Remove certain characters from string

From Dev

Remove repeating characters from string

From Dev

Remove characters from string with sed

From Dev

Remove all characters from the string

From Dev

remove specific characters from a string

From Dev

Remove nonalphabet characters from a string

From Dev

Remove characters from a string sql

From Dev

Attempting to remove characters from a string

From Dev

Remove last characters from string

From Dev

Remove Invalid Characters from string

From Dev

Remove element from array if string contains any of the characters

From Dev

Remove empty string characters from a javascript string

From Dev

Search from the string array and ignore that exact matched string

From Dev

Convert to string from an array of characters

From Dev

Remove Strings with same characters in a String Array

From Dev

Lodash remove from string array

From Dev

How to remove duplicated characters from string in Bash?

From Dev

Remove spaces and special characters from string

From Dev

Remove the last X specific characters from string

From Java

Remove specific characters from a string in Python

From Dev

Remove all special characters from string

Related Related

HotTag

Archive