As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

JavaScript Regular Expressions!

JasconiusJasconius sword criminalmad onlineRegistered User regular
edited May 2007 in Help / Advice Forum
I'm trying to pass a string into a function and weave it into a regular expression for use with the string search function.

Basically I'm trying to figure out the right syntax.

The hurdle here is that for some reason seach() only takes a regular expression as an argument.

I'm taking a file name, passing it into a function, and then checking the end of the current URL to see if it matches what was passed into the function.

For some reason, this version always comes up false, and I'm sure it has to do with my concatenation, but I just don't know how to concatenate a string INTO a regular expression, if it's even possible at all. Are they not two distinct data types?


The code is posted below.

var PageURL = document.URL;

function switcher(CaseName) {



	CaseName = "/" + CaseName + "$/"
	
	
	
	if (PageURL.search(CaseName) != -1){
		
			alert("true");
		
			}
		
	else	{
			
			alert("false")
			
			}
	
}

switcher("index.html");

Jasconius on

Posts

  • Options
    Jimmy KingJimmy King Registered User regular
    edited May 2007
    I can't play with it at the moment. Why not do an alert(PageURL.search(CaseName)) to see what is actually being returned? That might clue you in as to why it's not working.

    Jimmy King on
  • Options
    drinkinstoutdrinkinstout Registered User regular
    edited May 2007
    how about using indexOf and the lengths of the strings to match the two? or just a substring and an equals? I'm honestly not sure about regexes but those 2 ways should work

    drinkinstout on
  • Options
    JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited May 2007
    I don't know enough about JavaScript to know exactly what either of those are, I would have to see an example.

    Contextually, I am modifying SWFObject so that a designer (read:cannot program in ASP.NET) can just feed in an easy line(s) into the code so they can have what flash document they want on which page.

    Jasconius on
  • Options
    typhoontyphoon Registered User regular
    edited October 2021
    .

    typhoon on
  • Options
    drinkinstoutdrinkinstout Registered User regular
    edited May 2007
    ah okay, basically varname.indexOf('something') returns the location in varname that 'something' occurs: probably like what search does: so you know the length of both of the strings and you want to know when the variable matches at the end of your URL. You can try something like:

    if (PageURL.indexOf(CaseName) = (PageURL.length - CaseName.length))

    might have to adjust the comparisons by 1 on a side though

    you can also use a substring and try to match them but you'll have to do an additional check to make sure the lengths comply. so like:

    if (PageURL.length >= CaseName.length & PageURL.substring(PageURL.length - CaseName.length) = CaseName)

    drinkinstout on
  • Options
    JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited May 2007
    ah okay, basically varname.indexOf('something') returns the location in varname that 'something' occurs: probably like what search does: so you know the length of both of the strings and you want to know when the variable matches at the end of your URL. You can try something like:

    if (PageURL.indexOf(CaseName) = (PageURL.length - CaseName.length))

    might have to adjust the comparisons by 1 on a side though

    you can also use a substring and try to match them but you'll have to do an additional check to make sure the lengths comply. so like:

    if (PageURL.length >= CaseName.length & PageURL.substring(PageURL.length - CaseName.length) = CaseName)

    Yes, but that really doesn't make sense, we do web sites that have up to 200+ distinct pages, so some of those files are bound to have the same length... if I think that's what your implying.

    The above about the regular expression object is pretty much what I need. I can pass in a string to a reg express. Hurray!


    I believe that will be the solution. The way I check the URL may be crude but I'm certain it will work on any scale of a site.

    Thanks

    Jasconius on
  • Options
    drinkinstoutdrinkinstout Registered User regular
    edited May 2007
    the regular expression is probably the way to go since now you know how to use it but my examples aren't just comparing lengths :)

    drinkinstout on
Sign In or Register to comment.