Search code examples
javascriptregexwhitelistcharacter-limit

Javascript Regex Whitelist with character limit


I'm trying to add a character limit clause to this white list regex approach

str = str.replace(/[^A-Za-z-_0-9 ]/g, "");

Is it possible and how do I do it?


Solution

  • str = str.replace(/[^A-Za-z-_0-9\s]/g, "").substring(0,10);  //At most 10 chars