Regular expression to accept alpha numeric, underscore and accented characters

I have the following ASP/VBScript function that returns true if the given string contains alpha numeric characters or an underscore.

Function StringIsAlphaNumeric(TheString)
Dim re, IsAlphaNumeric

    Set re = New RegExp
    re.Pattern = "^\w*$"
    IsAlphaNumeric = re.Test(TheString)
    Set re = Nothing
    
    StringIsAlphaNumeric = IsAlphaNumeric

End Function

Open in new window


I would like to extend the regular expression so it also allows accented characters such as áéíóúÁÉÍÓÚ etc.

Currently this function returns false if I pass the following value:
abcáéíóú

What I do know is that I would like to allow the unicode range u00C8 to u00D6 but I do not know how to add this to the current regular expression.
mike99cAsked:
Who is Participating?
 
käµfm³d 👽Commented:
Didn't you and I already have this discussion  = )
0
 
käµfm³d 👽Commented:
Try it this way:
Function StringIsAlphaNumeric(TheString)
Dim re, IsAlphaNumeric

    Set re = New RegExp
    re.Pattern = "^[\w\xC8-\xD6]*$"
    IsAlphaNumeric = re.Test(TheString)
    Set re = Nothing
    
    StringIsAlphaNumeric = IsAlphaNumeric

End Function

Open in new window

0
 
mike99cAuthor Commented:
Hi kaufmed,

I just tested your solution and it does not work as it returns false for accented characters.

I have however modifed the script with a new regular expression to add the unicode range mentioned and it now works.

Function StringIsAlphaNumeric(TheString)
Dim re, IsAlphaNumeric

    Set re = New RegExp
    re.Pattern = "^\w[u00C0-\u00FC]*$"
    IsAlphaNumeric = re.Test(TheString)
    Set re = Nothing
    
    StringIsAlphaNumeric = IsAlphaNumeric

End Function

Open in new window

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
mike99cAuthor Commented:
Need to partially accept the solution from kaufmed.
0
 
mike99cAuthor Commented:
Solution was assisted from a previous one submitted by kaufmed so will partially accept.
0
 
mike99cAuthor Commented:
I had researched the previous question and it was my mistake not to have added the link in my original question. I thought I would simplify the whole thing by giving a clue as to what I wanted to add rather than confuse the issue by linking to another solution. I will know better next time.
0
 
käµfm³d 👽Commented:
@Qlemo

Excuse me sir, but I was not intending to pander for points. I was merely making a comment that I thought he had already asked something similar. Had I been pandering, I would have selected "Object" vs. "Post."

As point of fact, I recalled answering the question I linked to in the post above, but I couldn't find it at the time. This is why the solution I posted was inaccurate. I just happened to find the previous solution after I searched for both mine and mike99c's handles.

I am fine with the original resolution of the asker in closing the question without delegating points as my solution was inaccurate. I do ask the courtesy of not being accused of something that I don't feel I gave any hint of doing.

Thank you.
0
 
käµfm³d 👽Commented:
Vielen Dank.
0
 
mike99cAuthor Commented:
For closure, I would like to correct the script as follows:

 
Function StringIsAlphaNumeric(TheString)
Dim re, IsAlphaNumeric

    Set re = New RegExp
    re.Pattern = "^[\w\u00C0-\u00FC]*$"
    IsAlphaNumeric = re.Test(TheString)
    Set re = Nothing
    
    StringIsAlphaNumeric = IsAlphaNumeric

End Function

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.