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

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,"");

Related

Strip everything but letters and numbers and replace spaces that are in the sentence with hyphens

So I'm trying to parse a string similar to the way StackOverflow's tags work. So letters and numbers are allowed, but everything else should be stripped. Also spaces should be replaced with hyphens, but only if they are inside the word and not have disallowed characters before them.
This is what I have right now:
label = label.trim();
label = label.toLowerCase();
label = label.replace(/[^A-Za-z0-9\s]/g,'');
label = label.replace(/ /g, '-');
This works but with a few caveats, for example this:
/ this. is-a %&&66 test tag . <-- (4 spaces here, the arrow and this text is not part of the test string)
Becomes:
-this-is-a66-test-tag----
Expected:
this-is-a66-test-tag
I looked at this to get what I have now:
How to remove everything but letters, numbers, space, exclamation and question mark from string?
But like I said it doesn't fully give me what I'm looking for.
How do I tweak my code to give me what I want?
You need to make 2 changes:
Since you do not replace all whitespace with the first replace you need to replace all whitespace chars with the second regex (so, a plain space must be replaced with \s, and even better, with \s+ to replace multiple consecutive occurrences),
To get rid of leading/trailing hyphens in the end, use trim() after the first replace.
So, the actual fix will look like
var label = " / this. is-a %&&66 test tag . ";
label = label.replace(/[^a-z0-9\s-]/ig,'')
.trim()
.replace(/\s+/g, '-')
.toLowerCase();
console.log(label); // => this-isa-66-test-tag
Note that if you add - to the first regex, /[^a-z0-9\s-]/ig, you will also keep the original hyphens in the output and it will look like this-is-a-66-test-tag for the current test case.
Use trim just before changing all spaces with hyphens.
You can use this function:
function tagit(label) {
label = label.toLowerCase().replace(/[^A-Za-z0-9\s]/g,'');
return label.trim().replace(/ /g, '-'); }
var str = 'this. is-a %&&66 test tag .'
console.log(tagit(str));
//=> "this-isa-66-test-tag"

String replace regex character classes using regex

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

Remove trailing and leading white spaces around delimiter using regex

I am trying to remove white spaces from a string. However, I want to remove spaces around the delimiter and from beginning and ending of the string.
Before:
" one two, three , four ,five six,seven "
After:
"one two,three,four,five six,seven"
I've tried this pattern without success:
/,\s+|\s$/g,","
You could use /\s*,\s*/g, and then .trim() the string.
Use the regex ^\s+|(,)\s+|\s+(?=,)|\s$ and replace matches with the first capturing group $1:
var string = " one two, three , four ,five six,seven ";
console.log(string.replace(/^\s+|(,)\s+|\s+(?=,)|\s$/g, '$1'));
The capturing group is either empty or contains a comma when the regex engine encounters a space after a comma (,)\s+ (for which we would better use lookbehind, but JavaScript does not support it).

Removing new lines except when new line preceded by double space

I want to remove new lines from text, except when the sentence ends in a double space (I'm using JavaScript).
This:
This
is
a
test.
Should turn to this:
This is a test.
But this:
This
is //there is a double space here
a//but not here
test.
Should turn to this:
This is
a test.
My approach so far: I can replace multiple spaces followed by a new line with a single new line:
var doubleSpaceNewline = new RegExp(/(\s){2,}\n/g);
test = text.replace(doubleSpaceNewline, '\n');
But then how do I remove the newlines, without removing the one I want to remain?
I would prefer to remove all new lines except newlines preceded by double or more spaces, THEN replace double space + newline with single new line.
I need a regex that will match \s+ except when (\s){2,}\n. Can't seem to be able to combine both.
text = text.replace(" \n", '****************');
text = text.replace("\n", ' ');
text = text.replace('****************', " \n");
Is this what you're after? Doesn't use regex, but its a bit simpler of a procedure.
To find "one new line if not preceeded but 2 or more (judging by{2,} in your code) spaces" with the help of regular expressions, you can use negative lookbehind. Code for finding it is
(?<!\s{2,})\n
and then replace as usual.

replace empty space with dash only if trailing string

I'm using regEx and replace method to replace an empty space with a dash but I only want this to happen if there is a following character. For example
if the input field looked like this then replace empty space between temp and string with dash
input = "temp string";
if it looked like this then it would just remove the empty space
input = "temp ";
here is my regEx replace right now. But not sure how to check if there are trailing characters.
input.value.replace(/\s+/g, '-');
DEMO
input = $.trim(input.replace(/\b \b/g, '-'));
\b (word boundaries) info
jQuery.trim() api
This should do the trick:
the first replace takes care of the trailing spaces (if there's at least one)
the second one performs your original replacement
str.replace(/\s+$/g,'').replace(/\s+/g, '-');
DEMO
/\s+$/ only finds trailing spaces, so add .replace(/\s+$/, '') after .value

Categories