Solved

Javascript match

Posted on 2014-02-24
5
187 Views
Last Modified: 2014-02-24
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

0
Comment
Question by:9apit
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39882685
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/
0
 

Author Comment

by:9apit
ID: 39882931
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.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39882939
Yes, it return an array, you can use : << .toString() >> to convert it

Forget Document .write it old style javascript coding
0
 

Author Closing Comment

by:9apit
ID: 39882979
Hello Leak,

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

Thanks again.

Allen in Dallas
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39883013
- you append element :
parent.appendChild( document.createTextNode("hello") );
or
- you replace content of element :
element.innerHTML = "hello";
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now