I need to count length of string without spaces and tags.
My JS pattern doesnt work because it also not counts 'b' and 'r' chars.
My code is here:
content.match(/[^\s^<br />]/g).length
How to fix it?
Instead of a match, just use .replace(). Match always returns an array, and because primitives in Javascript are immutable, you can make a new string without those characters easily using replace().
let newString = oldString.replace(/\s/g, '') //replace all whitespace with empty spaces
newString = newString.replace(/<br\s*\/?>/g, '') //replace <br> and <br /> with empty spaces
and then just do newString.length
In the future, try using https://regexr.com to test your regex matching
If you wanted to remove all the HTML tags (not just <br/>), you could add your string as the HTML to a new element, grab the textContent, and then run a regex match on that.
let str = '<div>Hallo this is a string.</div><br/>';
let el = document.createElement('div');
el.innerHTML = str;
let txt = el.textContent;
let count = txt.match(/[^\s]/g).join('').length; // 19
DEMO
Related
Working with Javascript I need to be able to search a string input from a user and replace occurrences of semicolons with commas. Issue I have ran into is I need to be able to search the string for any commas that already exist, and quote around to the last and next occurrence of the semicolon.
Example:
User input is 12345;Joran,Michael;02;17;63 it should be converted to 12345,"Joran,Michael",02,17,63
My includes is able to locate the occurrence of a comma in the original string var srch = source.includes(","); and my replace is var converted = source.replace(/;/g, ","); which works fine, just need to figure out how to get to the last/next semicolon to place the quotes.
Using an if/else depending on if srch evaluates to True -- if true, add the quotes and then convert the rest of the string and return to the user; if false, convert and return.
I'm sure there's a way to do this with regex that just hasn't came to me yet so any suggestions on what to look at would be great.
I'd do this in two steps. First match non-; characters which have at least one ,, and surround them with quotes. Then replace all ;s in the result with ,:
console.log(
'12345;Joran,Michael;02;17;63'
.replace(/[^;,]*,[^;]*/g, '"$&"')
.replace(/;/g, ',')
);
Split the string by ;
.split(';')
which gives you an array.
Convert the elements that include a ',' to "${element}"
.map(s => s.includes(',') ? `"${s}"` : s )
Convert the array back to string
.join(',')
var str = '12345;Joran,Michael;02;17;63';
var arr = str.split(";");
var letters = /^[A-Za-z]/;
var final_str = "";
for (var i = 0; i < arr.length; i++) {
final_str = arr[i].match(letters)?final_str +'"'+ arr[i]+'"'+",":final_str + arr[i]+",";
}
console.log(final_str.substring(0,final_str.length -1));
I use
str.replace(/(^,)|(,$)/g, '')
to remove leading and trailing commas.
How can I extend it so I also remove two consecutive commas?
So ,some text,,more text, should become some text,more text?
One way would be to chain with
str.replace(/(^,)|(,$)/g, '').replace(/,,/g, ',')
but then ,some text,,,,more text, will become some text,,more text instead of some text,more text.
Since you appear to be using the str as a source for an array, you can replace all the .replace calls with:
var str = ",some text,,,,more text,";
var resultArray = str.split(',') // Just split the string.
.filter(function(item){ // Then filter out empty items
return item !== '';
});
console.log(resultArray)
No need to worry about leading, trailing or double comma's.
Remove the leading and trailing commas, and then replace multiple consecutive commas by single comma
str.replace(/^,|,$|(,)+/g, '$1');
,+ will match one or more comma, g-global flag to replace all occurrences of it.
var str = ',some text,,more text,';
str = str.replace(/^,|,$|(,)+/g, '$1');
console.log(str);
You may add an alternative branch and enclose it with a capturing group and then use a replace callback method where you can analyze the match groups and perform the replacement accordingly:
var s = ',some text,,,,more text,';
var res = s.replace(/^,|,$|(,+)/g, function(m,g1) {
return g1 ? ',' : '';
});
console.log(res);
To split with commas and get no empty entries in the resulting array, use a simple
console.log(',some text,,,,more text,'.split(',').filter(Boolean));
You could add a positive lookahead with another comma.
var str = ',some text,,more text,';
str = str.replace(/^,|,$|,(?=,)/g, '')
console.log(str);
What about one replace only like: ",some text,,,,more text,".replace(/(^,)|(,$)|,(?=,)/g, '');
[EDIT]
Note that lookbehinds don't work in javascript. so you can only use a lookahead like so.
I have a text in a textarea and get the value using the .val() attribute. I would like to remove the linebreaks (which is the doublespace)?
i tried using the .replace
sampleText = sampleText.replace(/(\r\n|\n|\r)/gm,"");
But it did not give me the correct solution.
sample text from my textarea
when i tried the .replace(), it will do like this
how do i remove the space between sample 2 and sample 3? it should look like this..
split by new line, filter out an empty line and finally join
sampleText = sampleText.split(/\n|\r/).filter(function(value){
return value.trim().length > 0;
}).join("\n");
Example
var sampleText = "Sample 1\nSample 2\n\nSample 3";
sampleText = sampleText.split("\n").filter(function(value){
return value.trim().length > 0;
}).join("\n");
document.write('<pre>'+sampleText+'</pre>');
You need to double up on your by using the + sign on your group filtering to only include the double occurences, and don't replace them with an empty string but with a new newline.
For more information about the plus sign I recommend to read http://www.regular-expressions.info/repeat.html
This way every double occurence will be replaced by a single occurence, which is what you want I guess
var sampleText = "Sample1\n\nSample2\n\r\n\r\r\r\nSample3";
document.write('<pre>Before:\n'+sampleText);
// The plus makes sure the matched pattern is repetitive and keeps replacing the doubles
sampleText = sampleText.replace(/(\r\n|\n|\r)+/gm,"\r\n");
document.write('\n\nAfter:\n'+sampleText+'</pre>');
You can do the replacement of two line breaks:
var sampleText = "Sample1\nSample2\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nSample3";
sampleText = sampleText.replace(/(\n){2,}/gm, "\n"); // matches 2 linebreaks to infinity;
document.querySelector('pre').innerHTML = sampleText;
<pre></pre>
Or just with .join() while creating an array out of string with .split():
var sampleText = "Sample1\nSample2\n\n\n\n\n\n\n\n\n\nSample3".split(/\n{2,}/gm).join('\n')
document.querySelector('pre').innerHTML = sampleText;
<pre></pre>
I am trying to remove some spaces from a few dynamically generated strings. Which space I remove depends on the length of the string. The strings change all the time so in order to know how many spaces there are, I iterate over the string and increment a variable every time the iteration encounters a space. I can already remove all of a specific type of character with str.replace(' ',''); where 'str' is the name of my string, but I only need to remove a specific occurrence of a space, not all the spaces. So let's say my string is
var str = "Hello, this is a test.";
How can I remove ONLY the space after the word "is"? (Assuming that the next string will be different so I can't just write str.replace('is ','is'); because the word "is" might not be in the next string).
I checked documentation on .replace, but there are no other parameters that it accepts so I can't tell it just to replace the nth instance of a space.
If you want to go by indexes of the spaces:
var str = 'Hello, this is a test.';
function replace(str, indexes){
return str.split(' ').reduce(function(prev, curr, i){
var separator = ~indexes.indexOf(i) ? '' : ' ';
return prev + separator + curr;
});
}
console.log(replace(str, [2,3]));
http://jsfiddle.net/96Lvpcew/1/
As it is easy for you to get the index of the space (as you are iterating over the string) , you can create a new string without the space by doing:
str = str.substr(0, index)+ str.substr(index);
where index is the index of the space you want to remove.
I came up with this for unknown indices
function removeNthSpace(str, n) {
var spacelessArray = str.split(' ');
return spacelessArray
.slice(0, n - 1) // left prefix part may be '', saves spaces
.concat([spacelessArray.slice(n - 1, n + 1).join('')]) // middle part: the one without the space
.concat(spacelessArray.slice(n + 1)).join(' '); // right part, saves spaces
}
Do you know which space you want to remove because of word count or chars count?
If char count, you can Rafaels Cardoso's answer,
If word count you can split them with space and join however you want:
var wordArray = str.split(" ");
var newStr = "";
wordIndex = 3; // or whatever you want
for (i; i<wordArray.length; i++) {
newStr+=wordArray[i];
if (i!=wordIndex) {
newStr+=' ';
}
}
I think your best bet is to split the string into an array based on placement of spaces in the string, splice off the space you don't want, and rejoin the array into a string.
Check this out:
var x = "Hello, this is a test.";
var n = 3; // we want to remove the third space
var arr = x.split(/([ ])/); // copy to an array based on space placement
// arr: ["Hello,"," ","this"," ","is"," ","a"," ","test."]
arr.splice(n*2-1,1); // Remove the third space
x = arr.join("");
alert(x); // "Hello, this isa test."
Further Notes
The first thing to note is that str.replace(' ',''); will actually only replace the first instance of a space character. String.replace() also accepts a regular expression as the first parameter, which you'll want to use for more complex replacements.
To actually replace all spaces in the string, you could do str.replace(/ /g,""); and to replace all whitespace (including spaces, tabs, and newlines), you could do str.replace(/\s/g,"");
To fiddle around with different regular expressions and see what they mean, I recommend using http://www.regexr.com
A lot of the functions on the JavaScript String object that seem to take strings as parameters can also take regular expressions, including .split() and .search().
I need a way to replace all appearances of <br class=""> with just <br>
I'm a complete novice with regex, but I tried:
str = str.replace(/<br\sclass=\"\"\s>/g, "<br>");
and it didn't work.
What's a proper regex to do this?
I would not use a regex to do this, but rather actually parse the html and remove the classes.
This is untested, but probably works.
// Dummy <div> to hold the HTML string contents
var d = document.createElement("div");
d.innerHTML = yourHTMLString;
// Find all the <br> tags inside the dummy <div>
var brs = d.getElementsByTagName("br");
// Loop over the <br> tags and remove the class
for (var i=0; i<brs.length; i++) {
if (brs[i].hasAttribute("class")) {
brs[i].removeAttribute("class");
}
}
// Return it to a string
var yourNewHTMLString = d.innerHTML;
One way is with the following
var s = '<br class="">';
var n = s.replace(/(.*)(\s.*)(>)/,"$1$3");
console.log(n)
\s matches exactly one whitespace character. You probably want \s*, which will match any number (including zero) of whitespace characters, and \s+, which will match at least one.
str = str.replace(/'<br\s+class=\"\"\s*>/g, "<br>");