INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

simple javascript postmatch not working

simple javascript postmatch not working

(OP)
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?

RE: simple javascript postmatch not working

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

RE: simple javascript postmatch not working

(OP)
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

RE: simple javascript postmatch not working

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

RE: simple javascript postmatch not working

(OP)
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

RE: simple javascript postmatch not working

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

RE: simple javascript postmatch not working

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close