function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kevoharakevohara 

Strange Javascript Regex Behavior in Visualforce Pages

I am having a really strange issue with String.match() javascript functions in visualforce pages.  Consider the following JS function...

 

function testRegex() {
  
  var str = 'Product A - $125.99';
  priceMatches = str.match(/\$(.*)/g);
  alert(priceMatches[0]);

}

 

When I call this function from the Firebug console when this function is in a <script> tag on the Visualforce page, the alert box is blank.  When I look at the object, it shows an array with a single blank value:

 

[ " " ]

 

I copied/pasted the exact same function into a plain html page on my desktop.  When I call that function from Firebug I get the expected alert:

 

$125.99

 

Has anyone else had problems with this?  I should note that my VF page has jQuery and jQuery UI running as well, but this should make no difference.

 

This is driving me nuts.  Any ideas?

kevoharakevohara

I just built a fresh VF page to make sure none of the other scripts were causing the issue.  The problem still persists.  Try this page, then try the same function in an html page.

 

<apex:page >
  <script type="text/javascript">
      function testRegex() {
            
            var str = 'Custom $955.45';
            var mat = str.match(/\$(.*)/g);
            alert(mat[0]);
            
        }
        
        window.onload = function() {
            testRegex();
        }
  </script>
</apex:page>

 

*werewolf**werewolf*

Note that JQuery defines an object called $ -- that might be confusing your regex.  I doubt that Visualforce itself has anything to do with the problem.

*werewolf**werewolf*

Hmm, well, if your second post is a page that has no JQuery then that rules that out.  Did you try stepping into each line of the code with Firebug to see if the regex definition itself is getting screwed up somehow?