Select Page

Art of BI: JavaScript – Find the Text Between Two Words

Christian Screen | | August 6, 2009

Using basic JavaScript and Regular Expressions I was looking for a way to parse any text string and capture only a segment of text within that string.  This is really a scenario of parsing. 

Logically to do what I needed one must know in advance what the preceeding and subsequent text or symbols of the desired text segment will be.  Typically this already pre-determined so that part was easy.  Once, I had that, I wrote a function using the “match” method of a JavaScript string variable to bump against the reg ex and poof the match method splits out an matching results into an indexed array.  I only want the first match so I call upon it to get my answers.

Here is the code, just wrap it in a <script> tag when you are ready to rock in roll.

[sourcecode lang=’javascript’]
var myText = “< ?xml version='1.0' encoding='utf-8'?>$j(‘#statusContainer’).slideDown(‘slow’);$j(‘#statusType’).removeClass(‘error’).addClass(‘good’);$j(‘#statusMessage’).html(‘Saved Successfully.’);cancelMainFormOperation();
approve sign up0

function parseReturnedXML(strToParse, strStart, strFinish)
alert(“start test”);

var str = strToParse.match(strStart + “(.*?)” + strFinish);

if (str != null) {
alert(“Parsed Item: ” + str[1]);
alert(“The string result returned null for the matching.nReturn blank, null, or the strToParse var if you want.”);

alert(“end test”);

And here is the function call below. Just place it in the body of your HTML in another <script> tag or below the actual function in the main HEAD section script tag.

[sourcecode lang=’javascript’]
parseReturnedXML(myText, ““, ““);


Super Cool Note

I am absolutely no stranger to JavaScript but I finally paid attention to something that some of you may or may not already know. When you declare a variable in JS using the var declaration you variable then becomes an object (ex: var str;). At which point you the ability to hold other object variable data such as the array that stems from another object. In the example of above “strToParse” becomes an object var as it is passed as an argument into the function. It is inherently an object by being an argument. However, if we did not declare the variable “str” using “var” (which is allowed) we would get an error when trying to evaluate the match method call from strToParse. Try it yourself. Remove the var declaration and run your script to view the result. You can also use your favorite IDE that has intellisense to view the methods an object has when declared with var and when it has not been declared using var.



In the example above I used a hard-code XML string of data inside the function, but did leave a parameter, strToParse, so that the text we want to parse can be passed in the function call.  XML for the string to parse is not necessary, I just wanted to show that this Reg Ex usage works not just for alphanumeric characters but also symbols, etc.  Manimuplate the code anyway you see fit to work with your applications.

Subscribe to Our Blog

Never miss a post! Stay up to date with the latest database, application and analytics tips and news. Delivered in a handy bi-weekly update straight to your inbox. You can unsubscribe at any time.

12c Upgrade Bug with SQL Tuning Advisor

This blog post outlines steps to take on Oracle upgrade 11.2 to 12.1 if you’re having performance problems. Oracle offers a patch and work around to BUG 20540751.

Megan Elphingstone | March 22, 2017

Oracle EPM Cloud Vs. On-Premises: What’s the Difference?

EPM applications help measure the business performance. This post will help you choose the best EPM solutions for your organization’s needs and objectives.

Bobby Ellis | April 10, 2018

Best RAID For SQL Server | RAID 0, RAID 1, RAID 5, RAID 10

Which RAID should you use with SQL Server? Learn the differences between RAID 0, RAID 1, RAID 5, and RAID 10, along with best practices.

Eric Russo | June 8, 2015

Work with Us

Let’s have a conversation about what you need to succeed and how we can help get you there.


Work for Us

Where do you want to take your career? Explore exciting opportunities to join our team.