• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

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?


Allen in Dallas

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

<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";


var match_result = message_whole.match(PRIORITY);




Open in new window

Allen Pitts
Allen Pitts
  • 3
  • 2
1 Solution
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/
Allen PittsBusiness analystAuthor Commented:
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
is an array.

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

Document .write will write the contents of an array

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")}
{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?


Allen in Dallas
If you want me to I will make this a separate question.
Yes, it return an array, you can use : << .toString() >> to convert it

Forget Document .write it old style javascript coding
Allen PittsBusiness analystAuthor Commented:
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") );
- you replace content of element :
element.innerHTML = "hello";
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now