Jump to content

Regular Experssion For "text\text"


DaNuGai
 Share

Recommended Posts

[A-Za-z0-9-_]{1,8}\\[A-Za-z0-9-_]+

This says

one to eighth alphanumeric characters or a "-" or "_", followed by a "\", followed by one or more alphanumeric characters or a "-" or "_"
If it doesn't work (as I'm a little uncertain about the "-" and "_"), try this as well:
([A-Za-z0-9]|\-|\_){1,8}\\([A-Za-z0-9]|\-|\_)+

Just curious, we are talking about NetBIOS domain names, right?

Edited by boen_robot
Link to comment
Share on other sites

Thanks for your reply. By the way, the regular expression you gave me doesn't seem to work properly.If the first string is longer than 8 characters, it still returns true.Also, I am only want to use alpha characters (no Numberic and no Special characters). I tried something like

([A-Za-z]){1,8}\\([A-Za-z])+

and it doesnt' work as well.Thanks again for your help. I appreciate it.

[A-Za-z0-9-_]{1,8}\\[A-Za-z0-9-_]+

This saysIf it doesn't work (as I'm a little uncertain about the "-" and "_"), try this as well:

([A-Za-z0-9]|\-|\_){1,8}\\([A-Za-z0-9]|\-|\_)+

Just curious, we are talking about NetBIOS domain names, right?

Link to comment
Share on other sites

Considering the fact we're talking about JavaScript, I have to ask... are you sure you've written the string correctly? You need to also escape the "\" in it, like so:

var regex = /^[A-Za-z]{1,8}\\[A-Za-z]+$/;var valString1 = "this\\valid";var valString2 = "domain\\thisIsAVeryLongUserName";var valString3 = "d\\t";var invString1 = "thisCouldBeValidIfOnlyMoreThanEighthCharactersWereAllowed\\thisToo";var invString2 = "domain\\\\ThisWasGoingToBeValidIfTwoSlashesWereAcceptable";var invString3 = "\\ThisWasGoingToBeValidIfTheDomainCouldBeEmpty";var invString4 = "domain\\";//Despite being a valid NetBIOS name, as per your request, the below is not acceptedvar invString5 = "my-server\\user";//both should be truealert(regex.test(valString1));alert(regex.test(valString2));alert(regex.test(valString3));//all three should be falsealert(regex.test(invString1));alert(regex.test(invString2));alert(regex.test(invString3));alert(regex.test(invString4));alert(regex.test(invString5));

Edited by boen_robot
Link to comment
Share on other sites

It's missing the start and end.^([A-Za-z]){1,8}\\([A-Za-z])+$^ means to match from the start of the string, and $ means to match until the end. The reason it was matching without that is because when you had a string like this:askldfjklasdjfklasjdf/askldfjdit was matching only this part:fklasjdf/askldfjdWith the start and end there, it means that it needs to match the entire string, not just part of it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...