How to replace all words of length less than 3 in a string with " " (space) in javascript?
Here is another approach which does two regex replacements. The first replacement strips words of length 2 or less, and the second replacement removes extra newline whitespace.
var input = "The First Backward Classes\nCommission was formed in\n1953 as chairman.\nJoachim Alva\nfi\nHardekar Manjappa\nKaka Kalelkar\n";input = input.replace(/\b\w{1,2}\b[ \t]*/sg, "").replace(/\n+(?=\n)/g, "");console.log(input);
Replace all spaces in a string with '+'
Here's an alternative that doesn't require regex:
var str = 'a b c';
var replaced = str.split(' ').join('+');
Shorten string without cutting words in JavaScript
If I understand correctly, you want to shorten a string to a certain length (e.g. shorten "The quick brown fox jumps over the lazy dog"
to, say, 6 characters without cutting off any word).
If this is the case, you can try something like the following:
var yourString = "The quick brown fox jumps over the lazy dog"; //replace with your string.
var maxLength = 6 // maximum number of characters to extract
//trim the string to the maximum length
var trimmedString = yourString.substr(0, maxLength);
//re-trim if we are in the middle of a word
trimmedString = trimmedString.substr(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(" ")))
Javascript all words are 3 characters or longer
You can use every
Before you can use every
the string need to be pre-processed.
trim
the string, remove the leading and trailing spaces.split
the string by one or more space characters- Then use
every
to check if length every element of the array is greater than or equal to three.
Demo
var string = 'all these words are three characters orr longer';
string.trim().split(/\s+/).every(function(e) { return e.length >= 3; });
JavaScript/jQuery: Replace 2nd, 3rd, etc space with but not the first one in text
str.replace(/ +/g, function(match) {
return " " + Array(match.length).join(' ');
});
This uses the version of replace
that takes a function to generate the substitution as the second parameter and replaces every occurrence of 2 or more spaces with a single space followed by the appropriate number of
.
There is a String.prototype.repeat
function but it isn't supported in IE so you might want to use an alternative way of generating the sequence of
for now. Array(match.length).join(' ')
is a short way to repeat a string taken from this answer to another question.
As an alternative you could use this function to perform the repeat, again taken from the Repeat String question:
function repeat(pattern, count) {
if (count < 1) return '';
var result = '';
while (count > 1) {
if (count & 1) result += pattern;
count >>= 1, pattern += pattern;
}
return result + pattern;
}
Notice with Array(n).join
you don't need to subtract 1 from the length of the match to get the right number of non-breaking spaces. e.g. Array(3).join(' ')
is " "
but for the other approaches to generating the repeated string you will need 1 less than the length of the match.
How to replace all dots in a string using JavaScript
You need to escape the .
because it has the meaning of "an arbitrary character" in a regular expression.
mystring = mystring.replace(/\./g,' ')
Split string to array of strings with 1-3 words depends on length
If we define words with length <6 to have size 1 and >=6 to have size 2, we can rewrite the rules to "if the next word would make the total size of the current row >= 4, start next line".
function wordSize(word) { if (word.length < 6) return 1; return 2;}let s = "Lorem ipsum dolor sit amet consectetur adipiscing elit sed doeiusd tempor incididunt ut Duis aute irure dolor in reprehenderit in esse cillum dolor eu fugia";var result = [];var words = s.split(" ");var row = [];for (var i = 0; i < words.length; ++i) { if (row.reduce((s, w) => s + wordSize(w), 0) + wordSize(words[i]) >= 4) { result.push(row); row = []; } row.push(words[i]);}result.push(row);result = result.map(a => a.join(" "));console.log(result);
Related Topics
How to Make My Discord Bot Send a Message When a Specific User Plays a Specific Game
Print Embedded Pdf from Browser With Javascript, Html5, Angularjs
Prevent Any Form of Page Refresh Using Jquery/Javascript
Make Discord Bot Send Picture With Message With Nodejs
Scrollable Table With Fixed Headers and Fixed First Column
Javascript Generate Video Thumbnail from Video Url
Javascript Backslash (\) in Variables Is Causing an Error
How to Format Numbers by Prepending 0 to Single-Digit Numbers
How to Sort Elements in an Array into Order and Even Numbers and They Should Be in Ascending Order
Add Hyphen After Every Fourth Character in a String Seperated by Comma
How to Find If a Text Contains Url String
Increase Counter Value Upon Button Click
How to Disable Eslint Rule Max Line Length for Paragraph in <Template> of Vue.Js
Regex to Match 2 Different Words in a String
React Passing Import Name as Prop and Adding to Image Src
Assigning Id to All Elements of a Page in Angular Automatically