Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple javascript postmatch not working

Status
Not open for further replies.

katie4335

Programmer
Jun 23, 2014
17
GB
i'm not that familiar with javascript. i'm trying to do something very simple which works fine on my local, but doesn't seem to work on a 3rd party environment (Google tag manager - macro)...

- I'm trying to match 'hello world' inside the var, 'code'
- then strip out any white space, to include carriage returns, new line breaks, etc.
- i think the issue could be somewhere around these two lines (as a check of str comes back as an object on my local, but null on the 3rd party environment)
Code:
var patt = /<h2 class="pic">My message<\/h2>(.*?)<\/div>/ig;
var str = patt.exec(code);
- when I debug further on my local, the value inside 'str[1]' is true and a string, and as expected would show up as empty on the 3rd part env

here's my code,

Code:
function (){

var code = '<h2 class="pic">My message</h2>hello world</div>'; 

var patt = /<h2 class="pic">My message<\/h2>(.*?)<\/div>/ig;
var str = patt.exec(code);
var result = str[1].replace(/^\s+|\s+$/ig,"");

return result;

}

Am i doing something wrong? is there a more efficient way to do this, guaranteed to work within any JavaScript environment?
 
What are you trying to match in the 3rd party environment. Seems like your Pattern is simply not matching anything over there.

In other words what is in your code variable there, and what does your pattern look like?

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
hey vacunita, here's the string,

<h2 class="pic">My message</h2>hello world</div>

and I'm trying to match 'hello world' by using this regex,

/<h2 class="pic">My message<\/h2>(.*?)<\/div>/ig

...on my local environment i'm successfully able to achieve this,

str[0] = <h2 class="pic">My message<\/h2>
str[1] = hello world
str[2] = hello world

the essential part for me is, (.*?) ..what's in between </h2> and </div>. it's the same that's on the 3rd party

so it bemuses me as to what i'm doing wrong/ why it won't work on the other environment
 
What's different between the "local" and the "production" environment?

Windows?
Linux?
BSD?
local javascript?
remote javascript?

and so on.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
the environment is through google tag manager. it's a snippet of iframe code that you place onto your site. through their user interface you then include the usual google analytics tracking tags, as well as custom js functions to track custom data. I don't think there are any limitations, well as far as i know. i even changed the var names in case they conflicted with some other names, but made no difference
 
And the rest of the questions?

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
hey chris, i got it working by changing this,

var str = patt.exec(code);

to this,

var str = code.match("<h2 class=\"pic\">My message<\/h2>\n(.*)<");
result = str[1];

my local is virtual linux via OS X, and the site is on linux
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top