How to replace part of a string using regex
You may use a regex that will match the first [....]
followed with [
and capture that part into a group (that you will be able to refer to via a backreference), and then match 1+ chars other than ]
to replace them with your replacement:
var str = "asd[595442/A][30327][0]";var strToReplace = "30333";console.log(str.replace(/(\[[^\]]*]\[)[^\]]*/, "$1" + strToReplace));
var strDesiredResult = "asd[595442/A][30333][0]";console.log(strDesiredResult);
javascript replace string after matching line
You can use this regex to match your first line:
/(<span id="lastModified">)(.*?)(</span>)/
It basically matches literally the text and captures it in three Groups, which is then used in the replacement pattern, which can be:
$12018-10-26$3
First it replaces with $1
(the first matched Group), then your date (which you will want to come from a variable) and finally $3
which is the end span.
Match and replace by regex, but do replace only for some part found by regexp
Note that you must define \
in a string literal with double \
. '[\s.,:\*]+'
=> '[\\s.,:*]+'
. Wrap it with a capturing group and use a backreference in the replacement. RegExp('([\\s.,:*]+)' + word + 's?i?\\s*:?\\s*', 'gi');
and replace with "$1"
, not ""
.
var s = "Some words .testword";var word="testword";var rx = RegExp('([\\s.,:*]+)' +word + 's?i?\\s*:?\\s*', 'gi');console.log(s.replace(rx, "$1"));
String replace with regex by using captured match as key
You can do that with callbacks:
const stringIhave = `<i id="1"></i><i id="2"></i><i id="3"></i>`;
const obj = { "1": "Red", "2": "Green", "3": "Blue"};
const stringIwant = stringIhave.replace(/id="(\d)"/g, (mat, grp) => `color="${obj[grp]}"`);
console.log(stringIwant);
regex replace only a part of the match
var input = "/09/small_image/09x/";var output = input.replace(/(\/\d+\/)small_image\/\d*x/, "$1thumbnail");console.log(output);
JavaScript REGEX Match all and replace
Well, first you'll need the g
flag on the regex. This tells JavaScript to replace all matched values. Also you can supply a function to the replace
method. Something like this:
var result = str.replace(/\{\{(.*?)\}\}/g, function(match, token) {
return data[token];
});
The second parameter matches the first subgroup and so on.
Regex: How to replace the matched text with a HTML element in JavaScript?
You could use a capturing group like this:
$1
indicates the text matched within the parentheses (capturing group) and can be used in replace
(MDN)
const str = "And here is the {{dog}} which is chasing the {{cat}}"const newStr = str.replace(/{{(\w+)}}/g, "<span class='$1' />")
console.log(newStr)
How to replace words in string by regex pattern with exception
You can use
const regex = /\b(?!google\b)[a-zA-Z0-9]+\.(?:com|net)\b/gi;
Details:
\b
- word boundary(?!google\b)
- a negative lookahead that fails the match if there isgoogle
as a whole word immediately to the right of the current position[a-zA-Z0-9]+
- one or more ASCII letters or digits\.
- a dot(?:com|net)
- a non-capturing group matching eithercom
ornet
\b
- a wor boundary.
Related Topics
Get Content of a Div Using JavaScript
Get Element's CSS Selector (When It Doesn't Have an Id)
How to Prevent Simultaneous Logins of the Same User with Firebase
Updating an Input's Value Without Losing Cursor Position
How to Join Two JavaScript Objects, Without Using Jquery
Unique Object Identifier in JavaScript
Changing the Default Title of Confirm() in JavaScript
Get Selected Text Position and Place an Element Next to It
Nested Element (Web Component) Can't Get Its Template
What Is Property in Hasownproperty in JavaScript
What's Pros and Cons: Putting JavaScript in Head and Putting Just Before the Body Close
Reason Behind This Self Invoking Anonymous Function Variant
Is There a Downside to Using Es6 Template Literals Syntax Without a Templated Expression
How to Read a Text File from Server Using JavaScript
How to Persist a Es6 Map in Localstorage (Or Elsewhere)
How to Reverse an Array in JavaScript Without Using Libraries