Javascript how to unify blank spaces into 1 blank space [duplicate] - javascript

This question already has answers here:
Regex to replace multiple spaces with a single space
(26 answers)
Closed 8 years ago.
So i have a dubt
if for example i have:
var string = "The String";
but i want string always to look "The String" (only 1 blank space in case of multiple sequenze of them)
how to do that in a clever way and dynamically, i mean there are many cases like these:
string = "This String";
string = "This String is short";
string = "This is the string";
i'm totally dumb in regexp (not only on it) and i guess it is the only way uh?

You should use a regex to get all spaces and replace it with one
string.replace(/\s\s+/g, " ");
If you only want it to work on a space and not tabs, use this:
string.replace(/ +/g, " ");
In the regex world "+" means 1 and any more that follow it. The "g" at the end means "global", or do it more than once. Removing the g would replace the first string of spaces but not any others. "\s" means all space-type characters which includes " " and tabs.

Related

Replace a string containing special characters [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
Trying to replace a string that contains special characters. The purpose of this is to convert the query string into an understandable format for end users.
full string is:
var str = 'active=true^opened_by=6816f79cc0a8016401c5a33be04be441^ORassigned_to!=6816f79cc0a8016401c5a33be04be441^short_descriptionISNOTEMPTY^NQopened_atONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday()^EQ';
Specifically the portion after ^NQ, in this example: opened_atONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday(). I have split the original string with indexOf(^NQ) and passing the resulting sub-strings to a function. I'm then trying a .replace() as below:
var today = replacementString.replace(/(ONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday())/g, ' is today ');
replacementString = today;
I have tried with various combinations of the above line, but have not returned what I am hoping for.
I've had no issues replacing special characters, or strings without special characters, but the combination of the 2 is confusing/frustrating me.
Any suggestions or guidance would be appreciated
You should escape the () to \(\) to match it literally or else it would mean a capturing group. For the match you could also omit the outer parenthesis and you have to escape the dot \. to match it literally.
ONToday#javascript:gs\.beginningOfToday\(\)#javascript:gs\.endOfToday\(\)
var str = 'active=true^opened_by=6816f79cc0a8016401c5a33be04be441^ORassigned_to!=6816f79cc0a8016401c5a33be04be441^short_descriptionISNOTEMPTY^NQopened_atONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday()^EQ';
var today = str.replace(/ONToday#javascript:gs\.beginningOfToday\(\)#javascript:gs\.endOfToday\(\)/g, ' is today ');
replacementString = today;
console.log(today);

javascript regex find words contains (at) in text [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
I've got a bunch of strings to browse and find there all words which contains "(at)" characters and then gather them in the array.
Sometimes is a replacement of "#" sign. So let's say my goal would be to find something like this: "account(at)example.com".
I tried this code:
let gathering = myString.match(/(^|\.\s+)((at)[^.]*\.)/g;);
but id does not work. How can I do it?
I found a regex for finding email addresses in text:
/([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi)
I think about something similar but unfortunately I can't just replace # with (at) here.
var longString = "abc(at).com xyzat.com";
var regex = RegExp("[(]at[)]");
var wordList = longString.split(" ").filter((elem, index)=>{
return regex.test(elem);
})
This way you will get all the word in an array that contain "at" in the provided string.
You could use \S+ to match not a whitespace character one or more times and escape the \( and \):
\S+\(at\)\S+\.\w{2,}

Remove excessive blank lines [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
Here is an attempt to remove any excessive blank lines in string.
I'm trying to understand why second approach doesn't workfor lines which contains whitespace.
Demo.
var string = `
foo
bar (there are whitespaced lines between bar and baz. I replaced them with dots)
....................
.......................
...........
baz
`;
// It works
string = string.replace(/^(\s*\n){2,}/gm, '\n');
// Why it doesn't work?
var EOL = string.match(/\r\n/gm) ? '\r\n' : '\n';
var regExp = new RegExp('^(\s*' + EOL + '){2,}', 'gm');
string = string.replace(regExp, EOL);
alert(string);
Your \s needs to be changed to \\s. Just putting \s is the same as s.
In strings (enclosed in quotes), the backslash has a special meaning. For example, \n is the newline character. There are a couple of others that you may or may not have heard of, e.g. \b, \t, \v. It would be bad language design choice to make only a few defined ones special, and consider the non-existent \s to be an actual backslash and an s, because it would be inconsistent, a source of errors, and not future-proof. That's why, when you want to have a backslash in a string, you escape the backslash to \\.
In your first example, you use / characters to delimit the regular expression. This is not considered a string bound by the above rules.

Javascript and Regex - Strict match of words in the string [duplicate]

This question already has answers here:
Javascript reg ex to match whole word only, bound only by whitespace
(5 answers)
Closed 6 years ago.
I have a string like this:
Free-coffee and coffee-free is free coffee
I need to match and replace only alone word free but not words {Free}-coffee and coffee-{free}
Idea is to mark bad words inside string and add HTML tag <strong> like this:
Free-coffee and coffee-free is <strong>free<strong> coffee.
I try with space but sometimes fail if before this sentence have space.
Here is my current regex:
/(\sfree\s|\sfree|free\s)/ig
NOTE: This need to be case insensitive.
And here is example of code:
var text = "Free-coffee and coffee-free is free coffee";
text = text.replace(/(\sfree\s|\sfree|free\s)/ig, " <strong>strong</strong> ");
Help me please.
Use the following regex pattern:
var text = "Free-coffee and coffee-free is free coffee";
text = text.replace(/(^|\s)(free)(\s|$)/ig, "$1<strong>$2</strong>$3");
console.log(text);
(^|\s) - ensures that free word is either at the start of the string OR preceded by space
(\s|$) - ensures that free word is either at the end of the string OR followed by space

Regex \s\s matches `return + place` [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 6 years ago.
I have constructed a string as follows
let str = String.fromCharCode(13) + ' ';
Next, I would like to know if the string contains two spaces
str.match(/\s\s/)
The weird thing is that it is a match. But if I do
str.match(/ /)
it isn't. Can someone explain to me why this is happening ?
The '\s' pattern allows you to match any kind of whitespace, not just space itself. For a more detailed list you can check here for example.
For reference (copied from the developer reference):
Matches a single white space character, including space, tab, form
feed, line feed and other Unicode spaces. Equivalent to [
\f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff].
For example, /\s\w*/ matches " bar" in "foo bar".

Categories