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)
Related
This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Closed 1 year ago.
How do I replace the first character of a string with another character? I am casting the number as string.
Example: String 12341
New string: 92341
I tried this script but cant figure a way to only replace the first character.
var oldStr =12341;
var newStr = oldStr.replace(1, 9);
oldStr is not a string, it's a number.
replace has to replace characters, not numbers.
So coerce the number to a string, and then do the replacement.
const oldStr = 12341;
const newStr = oldStr.toString().replace('1', '9');
console.log(newStr);
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.
This question already has answers here:
Trim specific character from a string
(22 answers)
Closed 3 years ago.
In Javascript, I have a generated string containing multiple lines, all ending in a newline, for example: "line1\nline2\nline3\n"
What is the best way to cut off the ending newline such that I get "line1\nline2\nline3" ?
For safety, it would be preferrable if the last character is only cut off if it actually is a newline. So if my input is "field1=5\nfield2=abc\nfield3= some string \n" I want to remove the last newline but keep the two spaces.
There are lots of general-purpose solutions out there, but JavaScript itself doesn't really contain a built-in specifically for removing a particular character. The replace() method combined with a regular expression, however, still does the trick just fine:
var string = "field1=5\nfield2=abc\nfield3= some string \n";
var trimmed = string.replace(/\n+$/, '');
Try:
var str = "line1\nline2\nline3\n"
if (str[str.length - 1] === '\n') str = str.substring(0, str.length - 1);
console.log(str);
This question already has answers here:
Replace multiple characters in one replace call
(21 answers)
Closed 3 years ago.
How to replace both colon and space with dash in regex?
Here's what I've managed to do:
to replace space: replace(/\s+/g, '-'),
to replace colon: replace(/:\s+/g, '-').
How do I merge these expressions?
You could do something like this:
var text = "hello: hey"
console.log(text.replace(/(:|\s+)/g, "-"))
Returns "hello--hey"
Use an alternation [ :]
var input = "Hello World:Goodbye";
console.log(input);
input = input.replace(/[ :]+/g, '-');
console.log(input);
Note that this replaces actual spaces, not all whitespace characters, which your original version using \s does.
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, " "));