Skip to content

Instantly share code, notes, and snippets.

@sakunyo
Created September 3, 2014 03:13
Show Gist options
  • Save sakunyo/6bb32a5c4606e6925831 to your computer and use it in GitHub Desktop.
Save sakunyo/6bb32a5c4606e6925831 to your computer and use it in GitHub Desktop.
var result;
// Case 1
/**
* location.href の値は使わずに result に値をセットする
*/
if (location.href.indexOf("/foo/") ) {
result = "foo";
}
// Case 2
/**
* 正規表現で文字列を完全に一致したものを使う
*/
var matches1;
if ((matches1 = /(foo|bar)/.exec(location.href)) && matches1.length > 0) {
result = matches1[1];
}
// Case 3
/**
* 正規表現で文字種[0-9a-zA-Z]など許可する文字を限定する
*/
var matches2;
if ((matches2 = /bar\/([0-9a-zA-Z]*)/.exec(location.href)) && matches2.length > 0) {
result = matches2[1];
}
console.log(matches1);
console.log(matches2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment