0

I'm trying to use variables in matching in javascript, but for some reason it's not working. In console, when I tried to alert, it says "ERROR: Execution of script 'test matching' failed! Cannot read property 'length' of null". What am I doing wrong?

// ==UserScript==
// @name        test matching
// @namespace   John Galt
// @description Basic Google Hello
// @match       *^https://www.google.com/$*
// @version     1
// @require     http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant       GM_xmlhttpRequest
// @run-at document-end
// ==/UserScript==

var temp = "findThis";
var string = "one two findThis three findThis four";

var tempArray = string.match("/"+temp+"/g");
var tempArrayCount = tempArray.length;
alert(tempArrayCount);

I need it for matching not replacing.

frosty
  • 2,559
  • 8
  • 37
  • 73
  • @mark meyer I saw that post but that's about replacing. Mine's about matching. How do I use it for matching? – frosty Aug 23 '18 at 21:26
  • 3
    it's still just making a RegEx with a variable: `string.match(new RegExp(temp, 'g'))` – Mark Aug 23 '18 at 21:29

0 Answers0