Regex to exclude numbers and special character except "-"

Samiksha2
Mega Sage

Hi All,

 

I have a client script to make first letter of every word as capital and articles like 'a', 'the'. etc in small letters. But in that I need to add more conditions.

1. numbers should not allowed 

2. special character should not allowed except "-"

 

Client script OnChange of u_new_user

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var myText = newValue;
var myTextInTitleCase = toTitleCase(myText);
g_form.setValue('u_new_user, myTextInTitleCase);


function toTitleCase(e) {
var t = /^(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|vs?\.?|via)$/i;
return e.replace(/([^\W_]+[^\s-]*) */g,
function(e, n, r, i) {
return r > 0 && r + n.length !== i.length && n.search(t) > -1 && i.charAt(r - 2) !== ":" && i.charAt(r - 1).search(/[^\s-]/) < 0 ? e.toLowerCase() : n.substr(1).search(/[A-Z]|\../) > -1 ? e : e.charAt(0).toUpperCase() + e.substr(1);
}
);
}

}

 

Thanks!

3 REPLIES 3

Claude DAmico
Kilo Sage

https://regex101.com/ is really nice for testing regex patterns. Using that site, I came up with /[^a-zA-Z0-9-]/gm as the regex pattern. The site also provides an area to test the expression and an explanation of how it is evaluating.

 

ClaudeDAmico_0-1665849963949.png

 

 
Match a single character not present in the list below [^a-zA-Z0-9-]
a-z matches a single character in the range between a and z (case sensitive)
A-Z matches a single character in the range between A and Z (case sensitive)
0-9 matches a single character in the range between 0 and 9 (case sensitive)
- matches the character - literally (case sensitive)
 
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Claude E. D'Amico, III - CSA

Thanks @Claude DAmico this is awesome.

But how I can add this expression in my code?

How do you want to handle when those characters exist? You stated they should not be allowed, but there are options. Remove, replace, return a message to the user and clear the value, etc.?

 

For example: I want "a new 123 brown cat jumped over THE - lazy, brown dog." to display as "A New Brown Cat Jumped Over the - Lazy Brown Dog" so just replace values as needed.

 

var myText = "a new 123 brown cat jumped over THE - lazy, brown dog."; //this would actually be newValue in your case
var myTextInTitleCase = toTitleCase(myText);
gs.info(myTextInTitleCase);
//g_form.setValue('u_new_user, myTextInTitleCase);

function toTitleCase(e) {
     var t = /^(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|vs?\.?|via)$/i;
     newText = e.replace(/([^\W_]+[^\s-]*)*/g, function(e, n, r, i) {/*gs.info("e is " + e); gs.info("n is " + n); gs.info("r is " + r); gs.info("i is " + i); */return r > 0 && r + n.length !== i.length && n.search(t) > -1 && i.charAt(r - 2) !== ":" && i.charAt(r - 1).search(/[^\s-]/) < 0 ? e.toLowerCase() : n.substr(1).search(/[A-Z]|\../) > -1 ? e : e.charAt(0).toUpperCase() + e.substr(1);});
     newText2 = newText.replace(/([^a-zA-Z-]+[\s])/g," ");
     return newText2;
}

 

Claude E. D'Amico, III - CSA