/
Regex for different use-cases

Regex for different use-cases

Regular expression:

A regular expression is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.

 

Remove span and strong tag from string

let re = /(<\s*strong[^>]*>)|(<\s*\/\s*strong>)|(<\s*span[^>]*>)|(<\s*\/\s*span>)/g let str = 'Hello <p>my</p> name <strong>is</strong>, <span style="color: rgb(17, 17, 17); font-family: &quot;Amazon Ember&quot;, Arial, sans-serif; font-size: medium;">Jone</span> is a company <strong>in India. <span>how </br>are' console.log(str.replace(re, ""))

 

Capital start char of sentence

function replaceAt(str, index, replacement) { return str.substring(0, index) + replacement + str.substring(index + replacement.length); } function isLetter(str) { return str.length === 1 && str.match(/[a-z]/i); } let re = /(<([^\/>]+)>)|(\.)/g let str = "<p>my name <b>jone my. name .my name</b> doe</p>" let newStr = str while ((match = re.exec(str)) != null) { console.log("match found at " + match.index, match, match[0].length); let nxtCharPos = match.index + match[0].length if(match[0] === "."){ if(str[nxtCharPos] === " "){ newStr = replaceAt(newStr, nxtCharPos+1, str[nxtCharPos+1].toUpperCase()) }else if(isLetter(str[nxtCharPos])){ newStr = replaceAt(newStr, nxtCharPos, str[nxtCharPos].toUpperCase()) } }else { newStr = replaceAt(newStr, nxtCharPos, str[nxtCharPos].toUpperCase()) } } console.log(">>>>>", newStr)

 

uppercase 1st letter after start tag

function replaceAt(str, index, replacement) { return str.substring(0, index) + replacement + str.substring(index + replacement.length); } let re = /<([^\/>]+)>/g let str = "<p>my name <b>jone</b> doe</p>" let newStr = str while ((match = re.exec(str)) != null) { console.log("match found at " + match.index, match, match[0].length); let nxtCharPos = match.index + match[0].length newStr = replaceAt(newStr, nxtCharPos, str[nxtCharPos].toUpperCase()) } console.log(">>>>>", newStr)

 

Capitalise First Letter in tag text

 

extract text from all <li> tags

 

 

Related content

Custom Code
More like this
Part of Speech Tagging Example
Part of Speech Tagging Example
More like this
Custom Code Functions and Examples
Custom Code Functions and Examples
More like this
UI String Standards
UI String Standards
More like this
Embedded Help Standards
Embedded Help Standards
More like this
April 2024, Release No. 75.01.00 - Release Notes
April 2024, Release No. 75.01.00 - Release Notes
More like this