I am learning regex, and I got a doubt. Let's consider
var s = "YYYN[1-20]N[]NYY";
Now, I want to replace/insert the '1-8' between [ and ] at its second occurrence.
Then output should be
YYYN[1-20]N[1-8]NYY
For that I had tried using replace and passing a function through it as shown below:
var nth = 0;
s = s.replace(/\[([^)]+)\]/g, function(match, i, original) {
nth++;
return (nth === 1) ? "1-8" : match;
});
alert(s); // But It wont work
I think that regex is not matchIing the string that I am using.
How can I fix it?
You regex \[([^)]+)\] will not match empty square brackets since + requires at least 1 character other than ). I guess you wanted to write \[[^\]]*\].
Here is a fix for your solution:
var s = "YYYN[1-20]N[]NYY";
var nth = 0;
s = s.replace(/\[[^\]]*\]/g, function (match, i, original) {
nth++;
return (nth !== 1) ? "[1-8]" : match;
});
alert(s);
Here is another way of doing it:
var s = "YYYN[1-20]N[]NYY";
var nth = 0;
s = s.replace(/(.*)\[\]/, "$1[1-8]");
alert(s);
The regex (.*)\[\] matches and captures into Group 1 greedily as much text as possible (thus we get the last set of empty []), and then matches empty square brackets. Then we restore the text before [] with $1 backreference and add out string 1-8.
If it’s only two occurences of square brackets, then this will work:
/(.*\[.*?\].*\[).*?(\].*)/
This RegEx has “YYYN[1-20]N[” as the first capturing group and “]NYY” as the second.
I suggest using simple split and join operations:
var s = "YYYN[1-20]N[]NYY";
var arr = s.split(/\[/)
arr[2] = '1-8' + arr[2]
var r = arr.join('[')
//=> YYYN[1-20]N[1-8]NYY
You can use following regex :
var s = "YYYN[1-20]N[]NYY";
var nth = 0;
s = s.replace(/([^[]+\[(?:[^[]+)\][^[]+)\[[^[]+\](.+)/, "$1[1-8]$2");
alert(s);
The first part ([^[]+\[([^[]+)\][^[]+) will match a string contain first sub-string between []. and \[[^[]+\] would be the second one which you want and the last part (.+?) match the rest of your string.
Related
I'm working on a drag and drop function for SVG path, which lets a user move the co-ordinates of the path.
Please consider the string below:
M162.323 150.513L232.645 8L303.504 149.837L461.168 173.5L347.156 284.5L373.605 440.728L233.5 367.854L91.7415 442L118.424 284.883L5.151 173.549Z
Would it be possible to replace a specific(let's say the 4th) occurence of a matched regex group using the .replace method?
Regex:
[A-Z](-?\d*\.?\d*\s-?\d*\.?\d*)
regex.exec() is a method that is used to find the next match in a string based on a regular expression. It returns an array containing the matched string and any capturing groups, or null if no match is found. This method can be used in a loop to iterate over all matches in a string and adjust the match accordingly.
let string = "M162.323 150.513L232.645 8L303.504 149.837L461.168 173.5L347.156 284.5L373.605 440.728L233.5 367.854L91.7415 442L118.424 284.883L5.151 173.549Z";
let regex = /[A-Z](-?\d*\.?\d*\s-?\d*\.?\d*)/g;
// Replace the 4th match
let newString = "";
let index = 0;
let match;
while (match = regex.exec(string)) {
if (index === 3) {
// Do something to modify the 4th match
newString += match[0].replace(/-?\d*\.?\d*\s-?\d*\.?\d*/, "REPLACED");
} else {
// Leave other matches unchanged
newString += match[0];
}
index++;
}
console.log(newString);
const s = 'M162.323 150.513L232.645 8L303.504 149.837L461.168 173.5L347.156 284.5L373.605 440.728L233.5 367.854L91.7415 442L118.424 284.883L5.151 173.549Z'
let n = 4, regex = /[A-Z](-?\d*\.?\d*\s-?\d*\.?\d*)/gm
console.log(s.replace(regex, m => --n ? m : 'hello'))
I want to do this in node.js
example.js
var str = "a#universe.dev";
var n = str.includes("b#universe.dev");
console.log(n);
but with restriction, so it can search for that string only after the character in this example # so if the new search string would be c#universe.dev it would still find it as the same string and outputs true because it's same "domain" and what's before the character in this example everything before # would be ignored.
Hope someone can help, please
Look into String.prototype.endsWith: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
First, you need to get the end of the first string.
var ending = "#" + str.split("#").reverse()[0];
I split your string by the # character, so that something like "abc#def#ghi" becomes the array ["abc", "def", "ghi"]. I get the last match by reversing the array and grabbing the first element, but there are multiple ways of doing this. I add the separator character back to the beginning.
Then, check whether your new string ends the same:
var n = str.endsWith(ending);
console.log(n);
var str = "a#universe.dev";
var str2 = 'c#universe.dev';
str = str.split('#');
str2 = str2.split('#');
console.log(str[1] ===str2[1]);
With split you can split string based on the # character. and then check for the element on position 1, which will always be the string after #.
Declare the function
function stringIncludeAfterCharacter(s1, s2, c) {
return s1.substr(s1.indexOf(c)) === s2.substr(s2.indexOf(c));
}
then use it
console.log(stringIncludeAfterCharacter('a#universe.dev', 'b#universe.dev', '#' ));
var str = "a#universe.dev";
var n = str.includes(str.split('#')[1]);
console.log(n);
Another way !
var str = "a#universe.dev";
var n = str.indexOf(("b#universe.dev").split('#')[1]) > -1;
console.log(n);
I have seen this link but my question is different.
javascript regex match all occurrences of substring?
match() function in JS can be used to match a substring in a string.
When I run this code, I got output like this.
let s = 'Hello_HaHaHaHackerRank';
let hacker = s.match('HaHa');
console.log(hacker);
console.log(hacker.index);
console.log(hacker.input);
Output:
["HaHa"]
6
Hello_HaHaHaHackerRank
hacker.index is giving one first occurrence of the pattern. But the string has three times HaHa. One is at index 6, another is at index 8 and another is at index 10.
Can anyone please explain, How can I get the occurrences of all substring?
indexOf has a fromIndex value you can use with a while loop str.indexOf(searchValue[, fromIndex])
let s = 'Hello_HaHaHaHackerRank';
let find = 'HaHa'
let hacker = [];
let i = 0, j=0;
while (~(i = s.indexOf (find,i + find.length))) hacker.push(i);
console.log (hacker)
If you want to include all occurences, don't add the length of the word.
while (~(i = s.indexOf (find,++i))) hacker.push (i)
Try with:
let re = /HaHa/g,
str = "Hello_HaHaHaHackerRank";
while ((match = re.exec(str)) != null) {
console.log("match found at " + match.index);
}
I have a string which I need to separate correctly:
self.view.frame.size.height = 44
I need to get only view, frame, size, and height. And I need to do it with a regular expression.
So far I've tried a lot of variants, none of them are even close to what I want to get. And my code now looks like this:
var testString = 'self.view.frame.size.height = 44'
var re = new RegExp('\\.(.*)\\.', "g")
var array = re.exec(testString);
console.log('Array length is ' + array.length)
for (var i = 0; i < array.length; i++) {
console.log('<' + array[i] + ">");
}
And it doesn't work at all:
Array length is 2
<.view.frame.size.>
<view.frame.size>
I'm new at Javascript, so maybe I want the impossible, let me know.
Thanks.
In Javascript, executing a regexp with the g modifier doesn't return all the matches at once. You have to execute it repeatedly on the same input string, and each one returns the next match.
You also need to change the regexp so it only returns one word at a time. .* is greedy, so it returns the longest possible match, so it was returning all the words between the first and last .. [^.]* will match a sequence of non-dot characters, so it will just return one word. You can't include the second . in the regexp, because that will interfere with the repetition -- each repetition starts searching after the end of the previous match, and there's no beginning . after the ending . of the word. Also, there's no . after height, so the last word won't match it.
EDIT: I've changed the regexp to use \w* instead of [^.]*, because it was grabbing the whole height = 44 string instead of just height.
var testString = 'self.view.frame.size.height = 44';
var re = /\.(\w*)/g;
var array = [];
var result;
while (result = re.exec(testString)) {
array.push(result[1]);
}
console.log('Array length is ' + array.length)
for (var i = 0; i < array.length; i++) {
console.log('<' + array[i] + ">");
}
If you're sure that your data will be always in the same format you can use this:
function parse (string) {
return string.split(" = ").shift().split(".").splice(1);
}
In your context, split is a MUCH better option:
var str = "self.view.frame.size.height = 44";
var bits1 = str.split(" ")[0];
var bits2 = bits1.split(".");
bits2.shift(); // get rid of the unwanted self
console.log(bits2);
I have a regexp:
/(alpha)|(beta)|(gamma)/gi
Some text to match against:
Betamax. Digamma. Alphabet. Hebetation.
The matches are:
beta, gamma, alpha, beta
The values I am looking would be:
1,2,0,1
...can I ascertain the index of the group that matched in the regexp?
To access the groups, you will need to use .exec() repeatedly:
var regex = /(alpha)|(beta)|(gamma)/gi,
str = "Betamax. Digamma. Alphabet. Hebetation.";
for (var nums = [], match; match = regex.exec(str); )
nums.push(match.lastIndexOf(match[0]));
If you want the indizes zero-based, you could use
nums.push(match.slice(1).indexOf(match[0]));
Build your regex from an array of strings, and then lookup the matches with indexOf.
If we consider the exact sample you provided, the below will work:
var r = /(alpha)|(beta)|(gamma)/gi;
var s = "Betamax. Digammas. Alphabet. Habetation.";
var matched_indexes = [];
var cur_match = null;
while (cur_match = r.exec(s))
{
matched_indexes.push(cur_match[1] ? 0 : cur_match[2] ? 1 : 2 );
}
console.log(matched_indexes);
I leave it to you to make the content of the loop more dynamic / generic :p