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
Code Block |
---|
|
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: "Amazon Ember", Arial, sans-serif; font-size: medium;">Taskmonk<>Jone</span> is a company <strong>in India. <span>how </br>are'
console.log(str.replace(re, "")) |
Capital start char of sentence
Code Block |
---|
|
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
Code Block |
---|
|
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
Code Block |
---|
|
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
let reg = /<([^>]+)>(.*?)<\/\1>/g
let reg2 = /(?<=<ul>)(.*?)(?=<\/ul>)/g
let reg3 = /(?<=<li>)(.*?)(?=<\/li>)/g
let str = "<li>john</li><li>Doe</li><li>jane</li><li>Doe</li>"
let str1 = "<ul><li>john is my name</li><li>Doe is Surname</li><li>@jane</li><li>Doe</li></ul>"
let str2 = str1.match(reg2)[0]
function replacer(match, offset, string) {
let replacedText = match.replace(match, capitalizeFirstLetter(match))
return replacedText;
}
let arr = str.replace(reg3, replacer)
let arr2 = str2.replace(reg3, replacer)
if(arr !== null){
console.log(str, ">>>", arr, "example2: ", str1, ">>>>", arr2)
} |
Code Block |
---|
|
let str = "<p><ul><li>hello this is ram</li><li>hello this is mohan</li></ul></p>"
let reg = /(?<=<li>)(.*?)(?=<\/li>)/g
let arr = str.match(reg)
if(arr !== null){
console.log([...arr])
} |