3

I would like to get whatever characters not matching the regex. E.g. "abc123" would return "123", [1,2,3] or something similar.

Is there any built in way to achieve this?

$('input').on('keyup', function(){

    var valid = /^[a-zA-Z\s*]*$/.test(this.value);
    console.log(valid);
});

http://jsfiddle.net/4JrWZ/

Another solution in case someone is intrested:

var x = this.value.split('').filter(function(v){
    return !/[a-zA-Z\s*]+/.test(v);
});

3 Answers 3

4

Using reduce

var result = "abc123def123".split(/[a-zA-Z\s*]+/).reduce(function(prev, curr) {
    return curr ? (prev.push(curr), prev) : prev;
}, []);

If you want the result as a string

var result = "abc123def123".split(/[a-zA-Z\s*]+/).reduce(function(prev, curr) {
    return curr ? prev + curr : prev;
}, "");
Sign up to request clarification or add additional context in comments.

9 Comments

@Johan Thanks :) I updated my answer with reduce variant as well :) Please check
Cool, didn't know about reduce :) Is there a way to make it either return a string or an array with each "invalid" character as an element? I know I can just to a result[0], but jsut curious. Oh and another tip: use [] rather than new Array() imo.
@Johan Please check the answer now. This version returns a string
I added another solution. Feel free to add it to your answer so I can remove it from my question.
@Johan You can write that like this var result = "abc123def123".split(/[a-zA-Z\s*]+/).filter(function(data) { return data ? data : false; }, ""); :)
|
1
/[^abc]+/.exec('abc123')

Returns the characters when they do not match either a, b or c.

Comments

0

how does this work with returning not accepted characters separated by comma? reduce will not work in below example example:

const allowedSplChars = /^([a-zA-Z0-9@\-._]{3,50})$/;
let allowedCharacters = allowedSplChars.test(uidInput.value);
var disallowedCharacters = [];
disallowedCharacters = uidInput.value.match(allowedSplChars);

if (allowedCharacters) {
  let splCharErrorNode = document.getElementById("uid_splCharacters");
  if (splCharErrorNode) {
    uidValidationcontainer.removeChild(splCharErrorNode);
  }
} else {
  //remove duplicates
  disallowedCharacters = disallowedCharacters.filter((element, index) => {
    return disallowedCharacters.indexOf(element) === index;
  });

  let splCharErrorNode = document.getElementById("uid_splCharacters");
  if (splCharErrorNode) {
    splCharErrorNode.innerHTML = `${disallowedCharacters.join(", ")}`;
  }
}

2 Comments

@thefourtheye , reduce does not work on string cahracters example *, %, how to extract all non matchings character list , not satifying regex
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.