Regex Parse of HTML

john_duchock
Kilo Guru

I have a string of "<title>This is a string</title>" and I need to set the values between the tags as a variable thus returning "This is a string" (without quotes of course)...

I tried this but it does not work...

var str = "<title>This is a string</title>";

var mySubString = str.substring(str.indexOf("<title>") +1, str.indexOf("</title>"));

gs.print(mySubString);

Am i going to have to Regex to get this value ? 

1 ACCEPTED SOLUTION

john_duchock
Kilo Guru

Ah...  got it:

var inText = current.variables.body_html;
var regex = /(?:\<title>)(.*)(?:\<)/g;
inText = regex.exec(inText);
var rslt = inText[1];
current.description = rslt;
current.short_description = rslt;

View solution in original post

3 REPLIES 3

john_duchock
Kilo Guru

Ah...  got it:

var inText = current.variables.body_html;
var regex = /(?:\<title>)(.*)(?:\<)/g;
inText = regex.exec(inText);
var rslt = inText[1];
current.description = rslt;
current.short_description = rslt;

How do you accomplish this is Flow Designer using the transform function regex?

Michael Jones -
Giga Sage

Generally speaking - yes. 

This is a little trick that works to strip out html tags without having to hard-code them individually. 

var str = "<title>This is a string</title>";

var mySubString = str.replace(/(<([^>]+)>)/gi, "");

gs.print(mySubString);

I hope this helps!

If this was helpful, or correct, please be kind and mark the answer appropriately.

Michael Jones - Proud member of the GlideFast Consulting Team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!