Link to home
Start Free TrialLog in
Avatar of Allen Pitts
Allen PittsFlag for United States of America

asked on

Javascript match

Hello expert,

I need to parse a string for the word PRIORITY.

I set up a test script to work with parsing a string
for the word, copied below. The code works if
i comment out the line that begins "var match_result..."

In the examples I have seen the syntax for the match method is
var match_result = message_whole.match(/PRIORITY);
but if I put the forward slash in it breaks.
So I tried
var match_result = message_whole.match(PRIORITY);
without the forward slash.
The script will run that way but I get no result.

Where am I going wrong?

Thanks.

Allen in Dallas



<HTML>
<HEAD>
<title> JS Match</title>
<style type="text/css">
div {font-family:arial;}
</style>
</HEAD 
<BODY>
<font face="arial" size="4">This writes
the message. <br />It is an example of inline Javascript.</font>

<div>
<script type="text/javascript">

var message1 = "MESSAGE: <br> ALERT: 2/21/2014 1:53:32 PM <br> NODE: LHPMSASSOLEOC <br>ASSIGNMENTGROUP: LHP-SYSTEMS<br>" ;
var message2 = "PRIORITY: 2<br> IMPACT: 2<br> CATEGORY: Hardware<br> SUBCATEGORY: Server<br> PRODUCTION_Y/N:Y <br> ";
var message3 = "ID: Alert me when a node goes down <br>"
var message4 = "ID: http://LHPMSASSOLSAM:80/Orion/Netperfmon/AckAlert.aspx?AlertDefID=9c52f683-07aa-4fd6-b9d5-5e2c387210bc:350:Node&viaEmail=true";
var message_whole = message1 + message2 + message3  + message4 + "<br> the whole message";

document.write(message_whole);

var match_result = message_whole.match(PRIORITY);

document.write(match_result);

</script>
</div>
</body>
</html>

</BODY>
</HTML>

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

You need to use /PRIORITY/ or /priority/i because you need a regular expression and not a string (in your example, PRIORITY is a null variable.

Test page : http://jsfiddle.net/W76MD/1/
Avatar of Allen Pitts

ASKER

Hello leak,

Thanks for your excellent answer.

Working on understanding why

var match_result = message_whole.match(/priority/i);
document.write= match_result;

won't work.

I think it is because  the result of
message_whole.match(/priority/i);
is an array.

Maybe that is why the var span is created.
Not sure.

Document .write will write the contents of an array
right?

Also the whole idea is to see if the word priority is in the string.
match() creates an array with a one item, the string PRIORITY.

Then I need the JavaScript to act based on PRIORITY being in the array
If (match_result == "PRIORITY")
{document.write= ("it is true")}
else
{document.write= ("it is not true")}

This doesn't work either I think because again
match_result is an array and "PRIORITY" is a string

How can I get an if statement, which uses Boolean to compare
the string against array contents?

Thanks.

Allen in Dallas
If you want me to I will make this a separate question.
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello Leak,

Thanks for the help.
If document.write is old style what is the new style?

Thanks again.

Allen in Dallas
- you append element :
parent.appendChild( document.createTextNode("hello") );
or
- you replace content of element :
element.innerHTML = "hello";