I want to cut this string into 50 characters where (after a space) it starts counting up to 50 (excluding spaces), and if the section is over 50 it inserts a space. So far, I can cut it up, but I don't know how to specify "exclude spaces". Tried [^\s] but no joy.
var str = " http://this.domain.com/fff/222/widget.css http://www.domain.com/myfolder/uploads/1/3/3/7/2332053/custom_themes/8787687678644/more_custom_themes/files/my-main_style.css?8763487634 http://cdn.domain.com/folder/images/thisfolder/common.css?9444"
str.replace(/\s(\w.{50})/g,' $1 ');
Try this:
var str = " http://this.domain.com/fff/222/widget.css http://www.domain.com/myfolder/uploads/1/3/3/7/2332053/custom_themes/8787687678644/more_custom_themes/files/my-main_style.css?8763487634 http://cdn.domain.com/folder/images/thisfolder/common.css?9444"
str = str.replace(/([^ ]{50})/g, "$1 ");
If your reason for doing this is to prevent a long string from breeaking out of its area in a browser window, you may want to try the CSS solution:
CSS has a word-wrap property, which you can use to tell the browser to break long words even if they don't have a natural break-point.
#divwithlongword {
word-wrap: break-word;
}
Now if you have HTML as follows:
<div id='divwithlongword'>myextraordinarilyandexcessivelylongdomainnamegoeshere.com</div>
...it will wordwrap where it needs to within the domain name.
This is supported in all major browsers -- see http://caniuse.com/#search=wordwrap
Hope that helps.
You use the following regex:
(\S{50})
\S - Matches any character that is not a whitespace character (spaces, tabs, line breaks).
Related
I have a short but complex regular expression to trim spaces regardless of html tags present in the string.
var text = "<span><span>ex ample </span> </span>";
// trim from start; not relevant in this example
text = text.replace(/^((<[^>]*>)*)\s+/g, "$1");
// trim from end
text = text.replace(/\s+((<[^>]*>)*)$/g, "$1");
console.log(text);
<span><span>ex ample </span> </span> - example input
<span><span>ex ample</span></span> - expected output
<span><span>ex ample </span></span> - observed output
How do I achieve my expected output?
I've tried adding the /g flag because it should supposedly match more than once and that should fix it (running the replace twice does work for the example) but it doesn't seem to repeat anything at all.
Alternative ways to trim strings regardless of tags are also appreciated because that is my primary objective. The secondary objective is learning why this didn't work.
You need to add some meaning to your tags, some need their spaces, some don't.
Try this:
text.replace(/\s*(<\/?(span|div)>)\s*/g, "$1")
.trim()
.replace(/\s+/g, ' ');
It:
replaces spaces around tags "surrounding" content
trims spaces around global string
removes redundant spaces
The list of "surrounding" tags can be changed to include things like tr...
Steps 2 and 3 might come first to speed things up.
Tried it with:
var text = "<div> <i>ano</i> <b>ther</b> <span> <b>my</b> <i>ex</i> <u> ample </u> </span> </div>";
First answer, prior to comments.
The idea is to remove all spaces between:
a non-space character and an opening tag
a closing tag and a non-space character
text.replace(/([^\s])\s*(<)/g, "$1$2")
.replace(/([>])\s*([^\s])/g, "$1$2")
.trim();
Preamble: don't just copy this, read to the end.
Thinking from the other way around - by replacing until no match is found instead of until no change is made, this seems to work very simply.
var text = "<span><span>ex ample </span> </span>";
var trim_start = /^((<[^>]*>)*)\s+/;
while(text.match(trim_start)) {
text = text.replace(trim_start, "$1");
}
var trim_end = /\s+((<[^>]*>)*)$/;
while (text.match(trim_end)) {
text = text.replace(trim_end, "$1");
}
console.log(text);
The output is as expected - the only space is between ex ample
But this has a big problem if the replace might not change anything. Simply changing \s+ to \s* makes it turn into an infinite cycle. So, all in all, it works for my case but is not robust and to use it, you must be completely sure every single replace will change something when the regex matches.
I'd like to replace the "&" character, along with characters that may interfere with urls syntax.
so far i tried:
myText = myText.replace(/[^a-zA-Z0-9-. ]/g,'');
that probably works for other characters (didn't test it) but didn't comprehend the "&" which is what i care most about, so i added in combo the following line but also didn't get rid of the &:
myText = myText.replace(/&/g,'');
but neither work, how can i replace this special character?
SOLUTION:
Code was reading & at delivery and not &, so i had to do:
myText = myText.replace(/&/g,'');
and it works.
SNIPPET:
var text = "god & damn it";
console.log(text.replace(/&|&/g,''));
According to your comments, what you are trying to replace is this &, the html encoding of the & character.
With lodash you can _.unescape the string before replacing:
myText = _.unescape(myText).replace(/&/g, '');
This way you handle both & and & cases. Then if you have to append that text in the html you should _.escape it back to prevent weird side effects: _.escape(myText);.
Without lodash you can just search both in your regex:
myText = myText.replace(/&|&/g, '');
But this method can have it's side effects when other special characters are present because it removes the & character too, for example this string "Three is > than two & one" would end up looking like this "Three is gt; than two one" (notice the ugly gt; in the middle)
console.log("m&yText".replace(/\&/g,''))
I can suggest adding the backslash character before the & as to 'escape' using the & as the regex character. You want the regex to find and replace any literal & character.
I have this html string x:
Michelle Brook
<br></br>
The Content Mine
<br></br>
michelle#contentmine.org
It is taken from first lines of http://www.dlib.org/dlib/november14/brook/11brook.html
I would like to obtain x.substring(0,14)=Michelle Brook.
The problem is that before the M, there are two special characters (unicode code=10) that makes x.substring(0,14)=Michelle Bro.
In fact, using x.split("") i can see {" "," ","M",.....}
I wouldn't remove these characters.
I would like to make substring doing the right thing "keeping in mind" that special characters. How could i do? Is there a different javascript function that makes that?
From your webpage:
window.onload = function() {
var arrStr = document.getElementsByClassName('blue')[0].innerHTML.replace(/[^A-Za-z0-9 <>]/g, '').split('<br>');
alert(arrStr[0].trim());
}
<p class="blue">
Michelle Brook<br>
The Content Mine<br>
michelle#contentmine.org<br><br>
Peter Murray-Rust<br>
University of Cambridge<br>
pm286#cam.ac.uk<br><br>
Charles Oppenheim<br>
City, Northampton and Robert Gordon Universities<br>
c.oppenheim#btinternet.com
<br><br>doi:10.1045/november14-brook
</p>
With the replace function you can remove any character is out of your interest:
in your case I considered you are looking for letters (uppercase, lowercase), numbers and space. You can add other characters to remove.
y cant u alote this strimg in a functio and trim start and end of the String.
Use .trim to remove \n (code 10)
The trim() method removes whitespace from both ends of a string.
Whitespace in this context is all the whitespace characters (space,
tab, no-break space, etc.) and all the line terminator characters (LF,
CR, etc.).
x.trim().substring(0,14);
Or using a regex:
var match = x.match(/[\w ]{14}/);
console.log(match[0]);
I have a string like that:
var str = 'aaaaaa, bbbbbb, ccccc, ddddddd, eeeeee ';
My goal is to delete the last space in the string. I would use,
str.split(0,1);
But if there is no space after the last character in the string, this will delete the last character of the string instead.
I would like to use
str.replace("regex",'');
I am beginner in RegEx, any help is appreciated.
Thank you very much.
Do a google search for "javascript trim" and you will find many different solutions.
Here is a simple one:
trimmedstr = str.replace(/\s+$/, '');
When you need to remove all spaces at the end:
str.replace(/\s*$/,'');
When you need to remove one space at the end:
str.replace(/\s?$/,'');
\s means not only space but space-like characters; for example tab.
If you use jQuery, you can use the trim function also:
str = $.trim(str);
But trim removes spaces not only at the end of the string, at the beginning also.
Seems you need a trimRight function. its not available until Javascript 1.8.1. Before that you can use prototyping techniques.
String.prototype.trimRight=function(){return this.replace(/\s+$/,'');}
// Now call it on any string.
var a = "a string ";
a = a.trimRight();
See more on Trim string in JavaScript? And the compatibility list
You can use this code to remove a single trailing space:
.replace(/ $/, "");
To remove all trailing spaces:
.replace(/ +$/, "");
The $ matches the end of input in normal mode (it matches the end of a line in multiline mode).
Try the regex ( +)$ since $ in regex matches the end of the string. This will strip all whitespace from the end of the string.
Some programs have a strip function to do the same, I do not believe the stadard Javascript library has this functionality.
Regex Reference Sheet
Working example:
var str = "Hello World ";
var ans = str.replace(/(^[\s]+|[\s]+$)/g, '');
alert(str.length+" "+ ans.length);
Fast forward to 2021,
The trimEnd() function is meant exactly for this!
It will remove all whitespaces (including spaces, tabs, new line characters) from the end of the string.
According to the official docs, it is supported in every major browser. Only IE is unsupported. (And lets be honest, you shouldn't care about IE given that microsoft itself has dropped support for IE in Aug 2021!)
Let's say I have the following string:
ZD:123123 ZD:213123 ZD:2e213 [ZD:213123] [ZD#221313] ZD:234...
I want to pattern match every occurrence except ZD:234... because I don't want any words that have an elipses.
This pattern was doing nicely for me in JavaScript:
/(\[|\(|)ZD[:#]\w+(\]|\)|)/g
However, it still captures the ZD:234 part of ZD:234... which I absolutely don't want it to do.
How can I prevent regex from doing this?
An easy fix is to use a negative lookahead:
/(\[|\(|)ZD[:#]\w+\b(\]|\)|)(?!\.\.\.)/g
Note that I've also added \b to avoid matching on ZD:23.
A bit simplified:
/[\[(]?ZD[:#]\w+\b[\])]?(?!\.\.\.)/g
In case you want matching brackets (no [ZD:123)):
/(?:ZD[:#]\w+|\[ZD[:#]\w+\]|\(ZD[:#]\w+\))\b(?!\.\.\.)/g
There is more than one way to skin a cat. The following will work in more browsers by using a simpler regular expression:
function trim(s) {
return s.replace(/^ | $/g,'').replace(/\s+/g,' ');
}
var x = 'ZD:123123 ZD:213123 ZD:2e213... [ZD:213123] [ZD#221313] ZD:234...';
alert(
trim(x.replace(/(^| )[^ ]+[\.]{3}( |$)/g,' ')).split(/\s+/)
);
/* shows: ZD:123123,ZD:213123,[ZD:213123],[ZD#221313] */
It removes any space delimited "word" of characters ending in ... and then splits on the space.