Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Regular Expression help

Featured Replies

Hi,

 

Probably not the first time this has been posted, but I get feeling this is slightly different from the usual expression question (but maybe not)... heres the deal...

 

window.onload = checksite;

function checksite(){

var setit = top.location;

function checkSsn(chknow){

var checking = "/*http:\/\/www.were.co.uk$/";

if (checking.test(chknow)) {

alert ("works");

}else{

alert ("failed");

}

}

 

checkSsn(setit);

}

 

 

I need to get a regular expression to check the "domain name" of the variable "siteloc".

 

say siteloc returns.... http://www.were.co.uk/somefile.php (not real address)..

I need a regular expression to check if the beginning of this string is "http://www.were.co.uk" , followed by anything after.

 

As soon as I add the following line... the script refuses to work and I cannot figuire out why!!

 

The killer:

 

if (checking.test(chknow)) {

alert ("works");

}else{

alert ("failed");

}

 

 

Can anyone shed some light for me please?

 

Thanks on advance

  • Author

also debugging says.... object doesn't support this property or method...?

You need to instantiate the variable called checking as a RegExp type variable:

 

var checking = new RegExp("/*http:\/\/www.were.co.uk$/");

 

Should work after that.

The error message gives it away... Because you've just declared checking as a normal var, it does not have a test() method.

  • Author

thanks leesalter and dizi....

 

Ok so the javascript does work now - however it is constantly returning "Failed" alert, meaning that the regular expression itself is not correct...

 

Anyone know what I'm doing wrong again?

 

 

window.onload = checksite;

function checksite(){

var setit = top.location;

function checkSsn(chknow){

var checking = new RegExp("/*http:\/\/www\.were\.co\.uk$/");

if (checking.test(chknow)) {

alert ("works");

}else{

alert ("failed");

}

}

 

checkSsn(setit);

}

Regular Expressions are definitely not a strong point of mine, but you could try:-

 

 var checking = new RegExp('^http:\/\/www\.were\.co\.uk\/*')

  • Author

weey. Ive been trying ever since, and couldn't get it to work!... but that worked, thanks.

 

I have 1 or 2 more bits I need to add to it, but I think I can handle this bit!!

 

Again thanks for you help, much appreciated!

  • Author

LeeSalter, again thanks for you help, I have now 100% completed my script :) (for now).

 

IF you are curious and would like to know what I used it for PM me, and i'll send you a link :)

No problem, webhost. Good to see that my 5 years of C#/ASP.NET/Web App development hasn't been entirely wasted! :-)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.