String replace regex character classes using regex - javascript

This string has regex character classes which need to be removed. As well as reduce multiple spaces to single space.
I can chain replace() but thought to ask if one can suggest a one regex code to do the whole job at one go. How can it be done? Thanks
"\n\t\t\t \n\n\t \n\t \t\tFood and drinks \n \t\n"
This is needed:
"Food and drinks"
var newStr = oldStr.replace(/[\t\n ]+/g, ''); //<-- failed to do the job

You want to remove all leading and trailing whitespace (space, tab, newline) but leave the spaces in the internal string. You can use the whitespace character class \s as shorthand, and match either the start or the end of the string.
var oldStr = "\n\t\t\t \n\n\t \n\t \t\tFood and drinks \n \t\n";
// ^\s+ => match one or more whitespace characters at the start of the string
// \s+$ => match one or more whitespace characters at the end of the string
// | => match either of these subpatterns
// /g => global i.e every match (at the start *and* at the end)
var newStr = oldStr.replace(/^\s+|\s$/g/, '');
If you also want to reduce the internal spaces to a single space, I would recommend using two regexes and chaining them:
var oldStr = "\n\t\t\t \n\n\t \n\t \t\tFood and drinks \n \t\n";
var newStr = oldStr.replace(/^\s+|\s+$/g, '').replace(/\s+/g, ' ');
After the first .replace() all of the leading and trailing whitespace is removed, leaving only the internal spaces. Replace runs of one or more space/tab/newline with a single space.
One other way to go could be to reduce all runs of whitespace to a single space, then trim the one remaining leading and trailing space:
var oldStr = "\n\t\t\t \n\n\t \n\t \t\tFood and drinks \n \t\n";
var newStr = oldStr.replace(/\s+/g, ' ').trim();
// or reversed
var newStr = oldStr.trim().replace(/\s+/g, ' ');
.trim() doesn't exist prior to ES5.1 (ECMA-262) but the polyfill is essentially .replace(/^\s+|\s+$/g, '') (with a couple of other characters added) anyway.

I'd recommend this pattern (assuming you want to keep \ns or \ts in your main string):
/^[\t\n ]+|[\t\n ]+$/g
If you don't want to keep them, you can use something like this:
/^[\t\n ]+|[\t\n]*|[\t\n ]+$/g

Related

How to remove characters in a string that matches a RegEx while preserving spaces?

I am trying to remove characters from a string so that it will match this RegEx: ^[-a-zA-Z0-9._:,]+$. For example:
const input = "test // hello". The expected output would be "test hello". I tried the following:
input.replace(/^[-a-zA-Z0-9._:,]+$/g, "")
But this does not seem to work
The example output "hello world" that you give does not match your regex, because the regex does not allow spaces. Assuming you want to keep spaces, use
input.replace(/[^-a-zA-Z0-9._:, ]/g, "")
The negation character ^ must be inside the [...]. The + is not needed, because /g already ensures that all matching characters are replaced (that is, removed).
If you also want to condense consecutive spaces into a single space (as implied by your example), use
input.replace(/[^-a-zA-Z0-9._:, ]/g, "").replace(/\s+/g, " ")
I like to use the following canonical approach:
var input = "test // hello";
var output = input.replace(/\s*[^-a-zA-Z0-9._:, ]+\s*/g, " ").trim()
console.log(output);
The logic here is to target all unwanted characters and their surrounding whitespace. We replace with just a single space. Then we do a trim at the end in case there might be an extra leading/trailing space.

JavaScript Regex - Remove Whitespace from Start and End

I worked on the below challenge for about 3 hours and none of my code was working. Decided to look at the solution to understand why I was not working. When I looked at the solution I was confused because I thought that \s to identify white spaces not to remove them... can someone give me hand and explain why the usage of \s instead of \S and why using the empty string ("") to get rid of the white spaces on both ends.
CHALLENGE
Write a regex and use the appropriate string methods to remove whitespace at the beginning and end of strings.
//SOLUTION
let hello = " Hello, World! ";
let wsRegex = /^\s+|\s+$/g;
let result = hello.replace(wsRegex, "");
\s means whitespace characters in regex, like <space>, <tab>, etc.
^ means the beginning of the string
$ means the end of the string
| means OR (match the left side or the right side)
+ means 1 or more (based off of the rule on the left)
/a regex/g the g means "global", aka "match multiple times" since you could need to match at the beginning AND end
So the regex means:
/^\s+|\s+$/g
/ / Wrap the regex (how you do it in JS)
^\s+ Try to match at the beginning one or more whitespace chars
| Or...
\s+$ Try to match whitespace chars at the end
g Match as many times as you can
String.prototype.replace replaces the match(es) found in the regex with the string provided as the 2nd argument, in this case an empty string.
So the process internally is:
Look for all sections that match the regex (which will be the whitespace at the beginning and the whitespace at the end
Replace each match with "", removing those matches entirely
let hello = " Hello, World! ";
let wsRegex = /^\s+|\s+$/g;
let result = hello.replace(wsRegex, "");
console.log('"' + result + '"');
Most people use String.prototype.replaceAll instead of .replace when they use the global flag (
let hello = " Hello, World! ";
let wsRegex = /^\s+|\s+$/g;
let result = hello.replaceAll(wsRegex, "");
console.log('"' + result + '"');
The second argument of replace is for what you will replace from the match(es) of the first argument.
The regex will match/select the spaces on the beginning (^) and on the end ($) of the string, and then will be replaced by "".
When you use the regex /(\S)/g you're matching everything but spaces, in this case you will use something like hello.replace(/(\S)/g, '$1');
$1 means the first group of your regex.

How to replace multiple characters from text?

As you can see in my code I am trying to replace some characters! So I want this:
First I remove all "||",
Then I remove all two or more white spaces with one space,
Then I replace all one space with "-"
With my code all two or more spaces are replacing with single space and all single spaces getting replace with "-". But the problem is this "|" is not getting removed. Please help!!
document.getElementById("NormalText").value.replace(/|/g, '').replace(/\s\s+/g, ' ').replace(/ /g, '-');
Your example isn't working because the pipe character has special meaning in regex; it is used for alternation. For example, a|c replaces all instances of a and c:
const myString = "abc";
const result = myString.replace(/a|c/g,"");
console.log(result);
To match a literal | character, escape it with a preceding backslash: \|:
const myString = "my|string";
const result = myString.replace(/\|/g, '');
console.log(result);
You need to escape the pipe symbol like so.
value.replace(/\|/g, '')

Regex replace punctuation with whitespace

I have a word counter function but it doesn't account for people using poor punctuation, for example:
"hello.world"
That would only count is as 1 word.
Instead it should count that as 2 words.
So instead I need a regex to replace comma's, full stops and any whitespace that is 1+ with a single whitespace.
Here's what I have so far:
proWords = proWords.replace(/[,\s]/, '\s');
negWords = negWords.replace(/[,\s]/, '\s');
The replacement is just an ordinary string, it shouldn't contain regular expression escape sequences like \s.
proWords = proWords.replace(/[,.\s]+/g, ' ');
The + regular expression makes it replace any sequence of the characters, and you need the g modifier to replace multiple times.
Change
proWords = proWords.replace(/[,\s]/, '\s');
negWords = negWords.replace(/[,\s]/, '\s');
to
proWords = proWords.replace(/[,\.\s]/, ' ');
negWords = negWords.replace(/[,\.\s]/, ' ');
This should work.

Regular expression to remove space in the beginning of each line?

I want to remove space in the beggining of each line.
I have data in each line with a set of spaces in the beginning so data appears in the middle, I want to remove spaces in the beginning of each line.
tmp = tmp.replace(/(<([^>]+)>)/g,"")
How can I add the ^\s condition into that replace()?
To remove all leading spaces:
str = str.replace(/^ +/gm, '');
The regex is quite simple - one or more spaces at the start. The more interesting bits are the flags - /g (global) to replace all matches and not just the first, and /m (multiline) so that the caret matches the beginning of each line, and not just the beginning of the string.
Working example: http://jsbin.com/oyeci4
var text = " this is a string \n"+
" \t with a much of new lines \n";
text.replace(/^\s*/gm, '');
this supports multiple spaces of different types including tabs.
If all you need is to remove one space, then this regex is all you need:
^\s
So in JavaScript:
yourString.replace(/(?<=\n) /gm,"");

Categories