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);
Related
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:
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)
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:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 3 years ago.
I have a string like
abcd/123/xyz/345
I want to replace every "/" with "-" using JavaScript.
The result string should be abcd-123-xyz-345
I have tried,
string.replace("/","-")
But it replaces the first "/" character only. The result is abcd-123/xyz/345
And
string.replace("///g","-");
is not working as well.
Is there any solution for this?
You can use Regex. You need to escape using backslash \ before the /.
A backslash that precedes a special character indicates that the next character is not special and should be interpreted literally
var str = 'abcd/123/xyz/345';
let result = str.replace(/\//g,'-');
console.log(result);
Please try this,
var str='abcd/123/xyz/345'
var newstr=str.split('/').join('-');
console.log(newstr)
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I'm trying to create regex for checking if password got special characters.
https://www.owasp.org/index.php/Password_special_characters
It looks like this
new RegExp('[!##\$%\^\&*\)\(+=._\'",/<>?[\\\]`{|}~:;-\s]', 'g');
Unfortunately is also catching bare words: reg.test('word') it returns true.
Whats wrong with my regex?
You didn't escape properly. RegExp object could receive a regular expression as a string but escaping matters: it needs double backslashes.
For now [;-\s] is equal to [;-s] which includes 57 characters:
[_;?\[\]#\\`\^<->aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsS-Z]
However, it should be [;\-\\s].
You can use negative/inverse logic and test against any character that is not a number or a letter.
Using [^A-Za-z0-9] where caret (^) matches everything except A-Za-z0-9.
const regex = /([^A-Za-z0-9]|[.\p{L}])/gm;
console.log('word: ' + regex.test('word'));
console.log('word!: ' + regex.test('word!'));
console.log('!£2word!: ' + regex.test('!£2word!'));
console.log('!ą,ć,ó!"£wordąćó: ' + regex.test('!ą,ć,ó!"£wordąćó'));