Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
var result = (function(x) {
  var matches,
      regex = /[\w]/gi;
      
  
  // when the regular expression is set on a variable or object a `lastIndex` property is set on it behind the scenes. So when the regular express contains the g (global) flag, it basically returns an array of matches and lastIndex will contain the index of the last match.
  
  console.log(regex.lastIndex); // 0
  
  matches = regex.test(x);
  console.log(matches); // true
  
  console.log(regex.lastIndex); // 1
  matches = regex.test(x);
  console.log(matches); // false, because there are no more matches since the value we tested with is only one letter and it already has been matched.
  
  return matches;
})('a');
console.log(result); // false
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
miguelmotapro
0viewers