How to extract hyperlink from input string and return as a list using semicolon separator using javaScript action. - Mendix Forum

How to extract hyperlink from input string and return as a list using semicolon separator using javaScript action.

3

In many scenarios we get input string where multiple hyperlinks are added as example given below.

[1]: TEST-17-Sect-07-ABC (5).pdf — page(s). [Page/1](https://abcsearch.blob.core.windows.net/raw/SF-17567-Sect-07-ABC%20(5).pdf?se=202CQLNlMOp90WpxbD7FUC88%3D#page=1),[Page/3](https://abcsearch..blob.core.windows.net/raw/SF-17-Sect-07-ABC%20(5).pdf?se=2024-06-21T14%3 JKGgGnxCQLNlMOp90WpxbD7FUC88%3D#page=3),[Page/5] (https://abcsearch..blob.core.windows.net/raw/SF-1678-Sect-07-ABC%20(5).pdf?se=2024-06-21T14%3 JKGgGnxCQLNlMOp90WpxbD7FUC88%3D#page=5)

in above example 3 hyperlink are added, in this scenario we need to display/use this hyperlink in our application. to achieve that we can use substring functionality in microflow if number of hyperlinks are limited but for multiple hyperlink it is good to use java script action in nanoflow if there are n number of hyperlink.

Step1: - Right click on folder and click add other javaScript action .

Step2:- Add parameter inpString as a string type.

Step3:- Go into code and add below 

==========================================

// This file was generated by Mendix Studio Pro.//// WARNING: Only the following code will be retained when actions are regenerated:// — the import list// — the code between BEGIN USER CODE and END USER CODE// — the code between BEGIN EXTRA CODE and END EXTRA CODE// Other code you write will be lost the next time you deploy the project.import “mx-global”;import { Big } from “big.js”;

// BEGIN EXTRA CODE// END EXTRA CODE

/** * @param {string} inpString * @returns {Promise.<string>} */export async function JS_GetImagePdfURL(inpString) { // BEGIN USER CODE

const regex = /(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0–9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0–9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0–9+&@#\/%=~_|$?!:,.]*\)|[A-Z0–9+&@#\/%=~_|$])/igm; const matches = inpString.match(regex);

if (matches) { return matches.join(‘;’); // Join the matches using semicolons } else { return ‘’; // Return an empty string if no matches found }  // END USER CODE}

==============================

Save the code and go to mendix studio pro and app-synchronize app directory and run the app locally.

Step4:-Now go into mendix nanoflow and call the API where you are getting the hyperlink in input string or getting the string as an input parameter.

call new java script action in Nanoflow as given below.

You will get the hyperlink in list using semicolon sperator. then we can use the string split to get the hyperlink and we can use as per our application requirements.

Posted
3 comments

Well explained!

Created

Thanks for sharing!!

Created

Good information! Thanks

Created
Thanks!!!
Created