How could I do something like this:
<script type="text/javascript">
$(document).ready(function () {
if(window.location.contains("franky")) // This doesn't work, any suggestions?
{
alert("your url contains the name franky");
}
});
</script>
like so:
Easier it gets
Regular Expressions will be more optimal for a lot of people because of word boundaries
\b
or similar devices. Word boundaries occur when any of0-9
,a-z
,A-Z
,_
are on that side of the next match, or when an alphanumeric character connects to line or string end or beginning.If you use
if(window.location.href.indexOf("sam")
, you'll get matches forflotsam
andsame
, among other words.tom
would match tomato and tomorrow, without regex.Making it case-sensitive is as simple as removing the
i
.Further, adding other filters is as easy as
Let's talk about
(?:\b|_)
. RegEx typically defines_
as aword character
so it doesn't cause a word boundary. We use this(?:\b|_)
to deal with this. To see if it either finds\b
or_
on either side of the string.Other languages may need to use something like
All of this is easier than saying
Other languages' flavors of Regular Expressions support
\p{L}
but javascript does not, which would make the task of detecting foreign characters much easier. Something like[^\p{L}](filters|in|any|alphabet)[^\p{L}]
Suppose you have this script
And the url form is
www.localhost.com/web_form_response.html?text_input=stack&over=flow
The text written to
<p id="response">
will bestack
Try this:
Put in your js file