Monday, 9 August 2021

Regex101 difference beteween [] vs [^] AND JS usage of regex

 [.......]  means to match anything provided in side of brackets

for example 




[^ .... ] means to match anything other than provided in side of brackets 




Javascript usage of abovce concept :

https://stackoverflow.com/questions/6555182/remove-all-special-characters-except-space-from-a-string-using-javascript

// Javascript string.replace('TargetInsideOfString', 'Replacement');

const string = "abc's test#s";
// In here TargetInsideOfString is regex pattern which is match anything in the provided brackets. 
// In this case special symbols provided in brackets, then those special symbol are replaced by ''
//  which means to remove them
string = string.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '');
// In here TargetInsideOfString is regex pattern which is match anything  
// other than the items in the  provided brackets. 
// In this case anything other than small case letter, upper case letter and 0-9 numbers
//  then those other things are replaced by '' 
// which means to remove them
string = string.replace(/[^a-zA-Z0-9]/g, '');






No comments:

Post a Comment