
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Adding paragraph tag to substrings within a string in JavaScript
We are required to write a JavaScript function that takes in a string str as the first argument and an array of strings, arr as the second argument. We need to add a closed pair of paragraph tag <p> and </p> to wrap the substrings in str that exist in arr. If two such substrings overlap, we need to wrap them together by only one pair of closed paragraph tag.
Also, if two substrings wrapped by paragraph tags are consecutive, we need to combine them.
For example −
If the input string and the array are −
const str = 'kkkllmm'; const arr = ["kkk","kkl","lm"];
Then the output should be −
const output = '<p>kkkllm</p>m';
Example
The code for this will be −
const str = 'kkkllmm'; const arr = ["kkk","kkl","lm"]; var addParagraphTag = (str = [], arr = []) => { if(!arr.length){ return str }; const { length } = str; let paraBoolean = new Array(length).fill(false); let end = 0; for (let i = 0; i < length; i++){ for (let j = 0; j < arr.length; j++){ let word = arr[j]; if (str.startsWith(word,i)) { end = Math.max(end, i + word.length); }; } paraBoolean[i] = end > i; }; let curr = 0; let newStr = ''; while (curr < length) { while(paraBoolean[curr] === false) { newStr += str[curr++]; } if (curr >= length) break; newStr += '<p>'; let startBold = curr; while (paraBoolean[curr] === true) curr++; newStr += str.slice(startBold, curr); newStr += '</p>'; }; return newStr; }; console.log(addParagraphTag(str, arr));
Output
And the output in the console will be −
<p>kkkllm</p>m
- Related Articles
- Find all substrings combinations within arrays in JavaScript
- Segregating a string into substrings - JavaScript
- Unique substrings in circular string in JavaScript
- Sorting alphabets within a string in JavaScript
- Performing shifts within a string in JavaScript
- Get all substrings of a string in JavaScript recursively
- Reversing words within a string JavaScript
- Is the string a combination of repeated substrings in JavaScript
- Get Equal Substrings Within Budget in C++
- Why does a stray HTML end tag generate an empty paragraph?
- Replacing Substrings in a Java String
- Counting all possible palindromic subsequence within a string in JavaScript
- C# Program to find all substrings in a string
- When is a CDATA section necessary within a script tag?
- Counting substrings of a string that contains only one distinct letter in JavaScript

Advertisements