How to replace each preceding whitespace with ? [duplicate] - javascript

This question already has answers here:
Match and replace all tabs at the beginning of the line with four spaces
(2 answers)
Closed 4 years ago.
I am trying to find a regex to replace each preceding whitespace of a string with "& nbsp;". Unfortunately I only find expressions that replace all whitespaces together.
Is there a regex for doing this?
var str = " test !";
console.log(str.replace(/(^\s+)(?=\b)/g, ' '));
// replaces all whitespaces with

Try this. str.replace(/ /gy, " ")
var str = " test !";
console.log(str.replace(/ /gy, " "));

Related

How to specify in regex to split : except within 2 double quotes [duplicate]

This question already has answers here:
Regex to match all instances not inside quotes
(4 answers)
Closed 2 years ago.
Subsequent to Why this regex does split second double quote but not first double quote?
I now added :, problem is I don't want to split if it is inside double quotes:
let regex = /(?=\.|[\":])/;
test = "test: \"test.test:\""
test.split(regex)
gives
["test", ": ", ""test", ".test", ":", """]
whereas I would like to have
["test", ": ", ""test", ".test:", """]
is it possible for regex (I'm not good at all at it)?
You may use this regex to match : only outside quotes (assuming quotes are all balanced and unescaped):
const test = "test: \"test.test:\""
var arr = test.split(/(?=[."])|:(?=(?:(?:[^"]*"){2})*[^"]*$)/)
console.log( arr )
Here, (?=(?:(?:[^"]*"){2})*[^"]*$) is a lookahead that asserts that we have even number of quotes ahead of current position.

Regex to format names [duplicate]

This question already has answers here:
Regex expression not working with once or none
(2 answers)
Closed 3 years ago.
Looking to find a regex that can remove characters and replace them with either a space, or with a comma and a space.
The problem is it ends up having 3 spaces, when I just want one. And if I try doing ", " I get 3 commas.
An example of how I want the output to be:
FirstName LastName, FirstName Lastname
var str = "&q=FirstNameLastName&q=FirstName2LastName2"
var newStr = str.replace(/[&q=]/g, ' ');
console.log(newStr)
Get rid of the square brackets. [&q=] matches any single character that's either &, q, or =, and then you replace each of them with a space, so you get 3 spaces. This will also replace q characters in the names.
Just write the string that you want to replace by itself.
var str = "&q=FirstNameLastName&q=FirstName2LastName2"
var newStr = str.replace(/&q=/g, ' ');
console.log(newStr)

How can I replace only plus sign (multiple occurrence) from a string without any text [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Replace all plus signs (+) with space in a string
(1 answer)
Closed 5 years ago.
How can I replace multiple (++++++) signs with blank space?
Example: var string = "+++++++++++++++++";
var string = "+++++++++++++++++";
string = string.replace(/\+*/g, '');
console.log(string.length);
const str = 'abc++++++def++++frberbf++fsvsf';
const newstr = str.replace(/(.)\+{2,}/g, ' ');
console.log(newstr);

Remove space in the middle of a string with $.trim? [duplicate]

This question already has answers here:
Regex to replace multiple spaces with a single space
(26 answers)
Closed 6 years ago.
I want remove the space in the middle of a string with $.trim() for example:
console.log($.trim("hello, how are you? "));
I get:
hello, how are you?
how can I get
hello, how are you?
Thanks.
You can use regular expression to replace all consecutive spaces \s\s+ with a single space as string ' ', this will eliminate the spaces and keep only one space, then the $.trim will take care of the starting and/or ending spaces:
var string = "hello, how are you? ";
console.log($.trim(string.replace(/\s\s+/g, ' ')));
One solution is to use javascript replace.
I recommend you to use regex.
var str="hello, how are you? ";
str=str.replace( /\s\s+/g, ' ' );
console.log(str);
Another easy way is to use .join() method.
var str="hello, how are you? ";
str=str.split(/\s+/).join(' ');
console.log(str);

How exactly does this RegEx and replace work in JavaScript? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
I have the following code:
var fileName = "C:\fakepath\a.jpg";
fileName = fileName.replace(/.*(\/|\\)/, '')
and this will return just the a.jpg as intended but I don't understand how it knew to replace the the characters between both of "\" such as the substring "fakepath". From what I see it should just replace the first character "C" because of the period and then any appearances of "/" or "\" with "".
The . means any character.
The * means zero or more.
So .* matches from the start of the string to the point where matching any more characters would prevent the rest of the regular expression from matching anything.

Categories

Resources