Search results

The swiss army knife for searching and replacing.… When the first argument of replace is a string, it only replaces the first match.… all occurrences of the string, while replace replaces only the first occurrence.… With g flag, it works the same as replace.… Let’s replace flag g with y in the example above.
Regular expressions are patterns that provide a powerful way to search and replace in text.… : str.replace.The method str.replace(regexp, replacement) replaces matches found using regexp in string… str with replacement (all matches if there’s flag g, otherwise, only the first one).… The second argument is the replacement… The method str.replace(regexp, replacement) replaces matches found using regexp with replacement: all
But if we need to fine-tune the replacement process, like to filter out circular references, we can use… Fortunately, we can use a function instead of an array as the replacer.… The function will be called for every (key, value) pair and should return the “replaced” value, which… The value of this inside replacer is the object that contains the current property.… The idea is to provide as much power for replacer as possible: it has a chance to analyze and replace
We can use the contents of capturing groups (...) not only in the result or in the replacement string… Don’t mess up: in the pattern \1, in the replacement: $1 In the replacement string we use
For instance, if a function has let userName, minifier replaces it with let a (or another letter if this… And inside the function, minifier replaces every mention of it.… They’re not just a dumb find-and-replace.
We can fix it by replacing \w with [\w-] in every word except the last one: ([\w-]+\.)+\w+.… Capturing groups in replacement.Method str.replace(regexp, replacement… ) that replaces all matches with regexp in str allows to use parentheses contents in the replacement… We can also use parentheses contents in the replacement string in str.replace: by the number $n or the… We also can’t reference such parentheses in the replacement string.
The exception is some selection methods, that replace the existing selection, such as setBaseAndExtent… input.setRangeText(replacement, [start], [end], [selectionMode]) – replace a range of text with the new… In its simplest one-argument form it replaces the user selected range and removes the selection.… In this example we find "THIS" in the input text, replace it and keep the replacement selected… If the selection is not empty, then it gets replaced (we can detect it by comparing selectionStart!
;user=Pete), has(name) – check for the existence of the parameter by name, set(name, value) – set/replace… Those that are not allowed, must be encoded, for instance non-latin letters and spaces – replaced with… As we can see, encodeURI replaced
boolean, if true then throw an exception for invalid (non-decodable) characters, otherwise (default) replace
For example, in the code above we could replace ??… In practice, the zero height is often a valid value, that shouldn’t be replaced with the default.
A switch statement can replace multiple if checks.
Replacing mouse<event> with pointer<event> in our code We can replace mouse<… this: And here’s the working logic, as it was described, after replacing… We can replace mouse with pointer in event names and expect our code to continue working for mouse, with
nodes or strings) –- insert nodes or strings after node, node.replaceWith(...nodes or strings) –- replaces… parentElem.replaceChild(node, oldChild) Replaces… node.after(...nodes or strings) –- insert right after node, node.replaceWith(...nodes or strings) –- replace
We have a text and need to replace all quotes "..." with guillemet marks: «...».… The first thing to do is to locate quoted strings, and then we can replace them.… Please note, that this logic does not replace lazy quantifiers! It is just different.
If someone is using it, there’s a good chance they can replace it with a modern language construct or
And, to make things more obvious, let’s replace \w with \d.… Unfortunately, that won’t help: if we replace \w+ with \w+?, the regexp will still hang.… If we replace one \d+ with two separate \d+\d+, nothing changes:
The example shows the contents of document.body and then replaces it completely:… Instead, it replaces it in the DOM.… In the line (*) we replaced div with <p>A new element</p>.… Instead it gets replaced with the new HTML in the outer context. nodeValue/data The content of a non-element
To emulate such behavior, a polyfill would need to analyze the code and replace all such operators with
We can’t replace a child by something else by assigning childNodes[i] = ....… Let’s modify one of the examples above: replace
If we assign something to elem.className, it replaces… property is rarely used, because such assignment removes all existing styles: it does not add, but replaces
Build a single file with all modules (or multiple files, that’s tunable), replacing native import calls… The resulting file is minified (spaces removed, variables replaced with shorter names, etc).… bundled together into a single file (or few files), import/export statements inside those scripts are replaced
In particular, if we replace the default prototype as a whole, then there will be no "constructor
especially when you use async/await (covered later in the chapter Async/await), but not a total replacement
Replace steps 4 and 5 with a single line that creates a Blob from all chunks:
of accessors is that they allow to take control over a “regular” data property at any moment by replacing
All we need to do is to perform a few replacements in the code above:… For instance, we can make the range object generate values asynchronously, once per second, by replacing
Recipe: factor out functions.Sometimes it’s beneficial to replace a code piece with a function, like
function() we can get a pseudo-array of its arguments as arguments, so func.call(this, x) should be replaced… Decorators and function properties.It is generally safe to replace a function or a method with a decorated
It can do everything: insert, remove and replace elements.… In the next example, we remove 3 elements and replace them with the other two:… A call to users.filter(army.canJoin, army) can be replaced with users.filter(user => army.canJoin(
Most of the time we can replace let by var or vice-versa and expect things to work:
The “switch” construct.The “switch” construct can replace multiple if checks.
interpreted as [<55349><56499>-<55349><56500>] (every surrogate pair is replaced
Don’t replace if with || or && Sometimes, people use the AND && operator
showAvatar() example from the chapter Promises chaining and rewrite it using async/await: We’ll need to replace
If we want some of them to be optional, then we’ll need to replace more . with ?..
If we replace Map with WeakMap, then this problem disappears.
variable: The proxy should totally replace… same without Reflect, for instance, reading a property Reflect.get(target, prop, receiver) can be replaced… No transparent replacement here.
Function properties can replace closures sometimes.
Why was __proto__ replaced by the functions getPrototypeOf/setPrototypeOf?
But this may help to replace Map with Set in certain cases with ease, and vice versa.
If we want a “default” value to replace
contents using: slotchange event – triggers the first time a slot is filled, and on any add/remove/replace
replace the value with something alike in the middle of a loop or a function.
Nowadays, there’s no need to use it, we can replace it with newer events, but it can often be found in
If you run the code below, it replaces the opener (current) window content with “Test”:
The cross-window messaging (explained soon below) is the suggested replacement.
Strings also have methods for doing search/replace with regular expressions.
That’s pretty convenient, but the wrapper is not perfect, it can’t replace events for all cases.… If there’s already a value with the same key, it will be replaced.
Usually, however, we don’t want to totally replace
Only first 50 results are shown.