Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expressions in Javascript

Status
Not open for further replies.

iaresean

Programmer
Mar 24, 2003
570
ZA
How would I use regular expressions in Javascript?

I have the following function:
function generateUser(){
var temp = window.document.domain_form.domainname.value;
var first = temp.split(".");
window.document.domain_form.login.value += first[0];
}

And I have the following code at the end of the domainname input box:
onChange="generateUser()";

Here is my problem:
I have one field called domainname, and another called login.
The user must enter a value for domainame and from this value I want to derive the login. Here are some examples of possible domain names:
mytest.com
second_test.de
stilltesting2.com

I only want the first eight characters, I don't want the .com or .de or .whatever and I don't want the http:// or the www. if they decide to write that out. If it starts with numbers (e.g. 46twenty.com) I would like to drop the numbers, and if it is only numbers (e.g. 46664.com) then I want to keep the login blank.

I have these two regular expressions that might work, but I don't know how to use them. I'm not exactly a reg expr wizz so I won't be surprised if the don't work.

Here they are:
/^(\w{,8})\.\w+$/

/^\d+(\w{,8})\.\w+$/

Please help me to get this working, any and all help is GREATLY appreciated. Thank you. :)
 
how about something like this?
Code:
<script type=&quot;text/javascript&quot;>
	function getFirstEight(s) {
		var matches = s.replace(&quot;.com&quot;, &quot;&quot;).match(/[a-z]+)+/gi);
		if (matches) return matches.join(&quot;&quot;).substring(0,8);
		else return &quot;&quot;;
	}
</script>

<input type=&quot;text&quot; onchange=&quot;alert(getFirstEight(this.value));&quot; />

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Well I tried this, but it didn't work, any ideas why?

function generateUser() {
var temp = window.document.domain_form.domainname.value;
var user = temp.replace(/http:\/\//gi,&quot;&quot;);
user = user.replace(/ user = user.replace(/^\d+/gi,&quot;&quot;);
user = user.replace(/..+/gi,&quot;&quot;);
var first = temp.split(&quot;.&quot;);
window.document.domain_form.login.value += user;
}

First replace is to remove the httpd://
Second replace is to remove the www.
Third replace is to remove any decimals
Fourth replace is to remove .com or .co.uk or .de etc

But it isn't working. I tried this in perl and it works ok, but I need it to work in javascript.

Any help is greatly appreciated.
 
A period or dot (.) is a wildcard. It means &quot;anything at all&quot;. So, if you replace .+ with blank, then nothing remains in your string. It doesn't work in Perl either. You need to escape your dots (eg: \.).

Sincerely,

Tom Anderson
Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top