Replace multiline text between two strings - javascript

I need to replace old value between foo{ and }bar using Javascript regex.
foo{old}bar
This works if old is a single line:
replace(
/(foo{).*(}bar)/,
'$1' + 'new' + '$2'
)
I need to make it work with:
foo{old value
which takes more
than one line}bar
How should I change my regex?

Change your regex to,
/(foo{)[^{}]*(}bar)/
OR
/(foo{)[\s\S]*?(}bar)/
so that it would match also a newline character. [^{}]* matches any character but not of { or }, zero or more times. [\s\S]*? matches any space or non-space characters, zero or more times non-greedily.

Related

(/\s+(\W)/g, '$1') - how are the spaces being removed?

let a = ' lots of spaces in this ! '
console.log(a.replace(/\s+(\W)/g, '$1'))
log shows lots of spaces in this!
The above regex does exactly what I want, but I am trying to understand why?
I understand the following:
s+ is looking for 1 or more spaces
(\W) is capturing the non-alphanumeric characters
/g - global, search/replace all
$1 returns the prior alphanumeric character
The capture/$1 is what removes the space between the words This and !
I get it, but what I don't get is HOW are all the other spaces being removed?? I don't believe I have asked for them to (although I am happy they are).
I get this one console.log(a.replace(/\s+/g, ' ')); because the replace is replacing 1 or more spaces between alphanumeric characters with a single space ' '.
I'm scratching my head to understand HOW the first RegEx /\s+(\W)/g, '$1'replaces 1 or more spaces with a single space.
What your regex says is "match one or more spaces, followed by one or more non-alphanumeric character, and replace that whole result with that one or more non-alphanumeric character". The key is that the \s+ is greedy, meaning that it will try and match as many characters as possible. So in any given string of spaces it will try and match all of the spaces it can. However, your regex also requires one or more non-word characters (\W+). Because in your case the next character after each final space is a word character (i.e. a letter), this last part of the regex must match the last space.
Therefore, given the string a b, and using parens to mark the \s+ and \W+ matches, a( )( )b is the only way for the regex to be valid (\s+ matches the first two spaces and \W+ matches the last space). Now it's just a simple substitution. Since you wrapped the \W+ in parentheses that makes it the first and only capturing group, so replacing the match with $1 will replace it with that final space.
As another example, running this replace against a !b will result in the match looking like a( )(!)b (since ! is now the last non-word character), so the final replaced result will be a!b.
Lets take this string 'aaa &bbb' and run it through.
We get 'aaa&bbb'
\s+ grabs the 3 spaces before the ampersand
(\W) grabs the ampersand
$1 is the ampersand and replaces ' &' with '&'
That same principal applies to the spaces. You are forcing one of the spaces to satisfy the (\W) capture group for the replacement. It's also why your exclamation point isn't nuked.
List of matches would be the following. I replaced space with ☹ so it is easier to see
"☹☹☹☹(☹)",
"☹☹☹☹(☹)",
"☹☹(!)",
"☹(☹)"
And the code is saying to replace the match with what is in the capture group.
' lots of☹☹☹☹(☹)spaces☹☹☹☹(☹)in this☹☹(!)☹(☹)'
so when you replace it you get
' lots of☹spaces☹in this!☹'

Removing non-alphanumeric text with String.prototype.replace

I'm trying to strip a string of all characters that are not a letter or a number. I tried String.prototype.replace with a regular expression, but it didn't remove the expected characters:
this.colorPreset1 = this.colorPreset1.replace(/^[0-9a-zA-Z]+$/, '');
this.colorPreset1=this.colorPreset1.replace(/[^0-9a-zA-Z]/g, '');
The character group was changed to a exclusion group. [^] will match any character not in the list. As you had it, it would only match the characters you wanted to keep.
The anchors for the string were removed - You're wanting to replace any non-alpha numeric characters, so it doesn't matter where they're located.
The global flag //g was added so it will replace all matches instead of just the first one.
By adding ^ and $ around your regular expression, you explicitly tell it to match strings starting and ending with this pattern.
So it will replace the searched pattern only if if all the content of the string matches the pattern.
If you want to match each occurence of non numerical or alphabetical characters, you will have to remove the ^ start constraint and the $ end constraint, but also will have to change the pattern itself:
[A-Za-z0-9]
matches alphabetical or numerical characters, you want the opposite of that (to inverse a character class add a ^ at the start of the character class:
[^A-Za-z0-9]
finally add the g option to the regex to tell it to match each occurence (otherwise only the first occurence will be replaced):
/[^A-Za-z0-9]+/g
JavaScript RegEx replace will only replace the first found value. If you specify the g argument in your pattern, it denotes Global or "replace all."
this.colorPreset1=this.colorPreset1.replace(/[^0-9a-zA-Z]/g, '');
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
What does the regular expression /_/g mean?

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).

How to remove a substring between two specific characters

So I have a string:
"this is the beginning, this is what i want to remove/ and this is the end"
How do I use Javascript to target the string between the comma and the forward slash?
(I also want to remove the comma and the slash)
string = string.replace( /,.*?\//, '' );
In the regexp, , matches itself, .*? matches any sequence of characters (preferring the shortest match, due to the ? modifier) and \/ matches a slash. (The slash needs to be escaped with a backslash because it's also used as the regexp delimiter. Unfortunately, JavaScript doesn't support alternative regexp literal delimiters, so you'll just have to cope with the leaning toothpick syndrome.)
Note that the code above removes everything from the first comma to the first following slash in the string. Depending on what you want to happen if there might be multiple commas or slashes, there are various ways to modify the behavior of the regexp. For example:
.replace( /,.*\//, '' ) removes everything from the first comma to the last slash;
.replace( /^(.*),.*?\//, '$1' ) removes everything from the last comma followed by a slash up to the next slash;
.replace( /^(.*),.*\//, '$1' ) removes everything from the last comma followed by a slash up to the last slash in the string;
.replace( /,[^\/]*\//, '' ) does the same as the original regexp, i.e. removes everything from the first comma to the first following slash (even if there are other commas in between);
.replace( /,[^,\/]*\//, '' ) removes the first substring that begins with a comma and ends with a slash, and does not contain any commas or slashes in between;
.replace( /^(.*),[^,\/]*\//, '$1' ) removes the last substring that begins with a comma and ends with a slash, and has no commas or slashes in between; and
.replace( /^,[^,\/]*\//g, '' ) removes all substrings that begin with a comma and end with a slash, and have no commas or slashes in between.
Also note that, due to a historical quirk of regexp syntax, the . metacharacter will actually match any character except a newline ("\n"). Most regexp implementations provide some way to turn off this quirk and make . really match all characters, but JavaScript, for some reason, doesn't. If your string might contain newlines, you should replace all occurrences of . in the regexps above with some workaround such as [\s\S] (works in most PCRE-style regexp engines) or [^] (shorter, but specific to JavaScript's regexp flavor).
As your question is tagged with regex, you seem to want .replace:
return str.replace(/, this is what i want to remove\//, "");
If you don't know the string in between, use
/,(.+?)\//
or with a * instead of the + if the string could also be empty
With string functions, it would be
var cpos = str.indexOf(","),
spos = str.indexOf("/");
if (cpos > -1 && spos > cpos)
return str.substr(0, cpos)+str.substr(spos+1);
return str.split(',')[1].split('/')[0];
var commaIndex=str.indexOf(',')
var slashIndex=str.indexOf('/')
var finalString=str.substring(commaIndex,slashIndex)
Hope it will work

Regular Expression - Match any character except +, empty string should also be matched

I am having a bit of trouble with one part of a regular expression that will be used in JavaScript. I need a way to match any character other than the + character, an empty string should also match.
[^+] is almost what I want except it does not match an empty string. I have tried [^+]* thinking: "any character other than +, zero or more times", but this matches everything including +.
Add a {0,1} to it so that it will only match zero or one times, no more no less:
[^+]{0,1}
Or, as FailedDev pointed out, ? works too:
[^+]?
As expected, testing with Chrome's JavaScript console shows no match for "+" but does match other characters:
x = "+"
y = "A"
x.match(/[^+]{0,1}/)
[""]
y.match(/[^+]{0,1}/)
["A"]
x.match(/[^+]?/)
[""]
y.match(/[^+]?/)
["A"]
[^+] means "match any single character that is not a +"
[^+]* means "match any number of characters that are not a +" - which almost seems like what I think you want, except that it will match zero characters if the first character (or even all of the characters) are +.
use anchors to make sure that the expression validates the ENTIRE STRING:
^[^+]*$
means:
^ # assert at the beginning of the string
[^+]* # any character that is not '+', zero or more times
$ # assert at the end of the string
If you're just testing the string to see if it doesn't contain a +, then you should use:
^[^+]*$
This will match only if the ENTIRE string has no +.

Categories