Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript match

Posted on 2014-02-24
5
Medium Priority
?
205 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:Allen Pitts
  • 3
  • 2
5 Comments
 
LVL 83

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:Allen Pitts
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 83

Accepted Solution

by:
leakim971 earned 2000 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:Allen Pitts
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 83

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

886 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