I tried to replace [[ with ${.
var str = "it is [[test example [[testing";
var res = str.replace(/[[[]/g, "${");
I am getting the result "it is ${${test example ${${testing" but I want the result "it is ${test example ${testing".
Your regex is incorrect.
[[[]
will match one or two [ and replace one [ by ${.
See Demo of incorrect regular expression.
[ is special symbol in Regular Expression. So, to match literal [,
you need to escape [ in regex by preceding it \. Without it [ is treated as character class.
var str = "it is [[test example [[testing";
var res = str.replace(/\[\[/g, "${");
// ^^^^
document.write(res);
you want to escape the [ using \
var res = str.replace(/\[\[/g, "${");
Just problem with escape characters.
use \ before [.
var str = "it is [[test example [[testing";
var res = str.replace(/\[\[/g, "${");
If you don't want to use regex
var res = str.split('[[').join('${');
Sample Here:
var str = "it is [[test example [[testing";
var res = str.split('[[').join('${');
document.write(res);
Related
I'm using the regex to search for all instances of the string that matches Hello[n] pattern.
var str = 'Hello[0] hello[2] hell Welcome to JavaScript.';
var regex = /hello+/gi;
var result = str.match(regex);
The code above produces the following outcome.
[ 'Hello', 'hello' ]
I want to know how to modify my regex to produce the following result.
[ 'Hello[0]', 'hello[1]',..... ]
If you want to include the number, you've to change the Regex to hello\[\d+\]+.
Working example: https://regex101.com/r/Xtt6ds/1
So you get:
var str = 'Hello[0] hello[2] hell Welcome to JavaScript.';
var regex = /hello\[\d+\]+/gi;
var result = str.match(regex);
Extend your current regex pattern to include the square brackets:
var str = 'Hello[0] hello[2] hell Welcome to JavaScript.';
var matches = str.match(/hello\[.*?\]/gi);
console.log(matches);
var str = 'Hello[0] hello[2] hell Welcome to JavaScript.';
var regex = /Hello\[[0-9]+\]+/gi ;
var result = str.match(regex)
console.log(result)
Is there an easy way to make this string:
(53.5595313, 10.009969899999987)
to this String
[53.5595313, 10.009969899999987]
with JavaScript or jQuery?
I tried with multiple replace which seems not so elegant to me
str = str.replace("(","[").replace(")","]")
Well, since you asked for regex:
var input = "(53.5595313, 10.009969899999987)";
var output = input.replace(/^\((.+)\)$/,"[$1]");
// OR to replace all parens, not just one at start and end:
var output = input.replace(/\(/g,"[").replace(/\)/g,"]");
...but that's kind of complicated. You could just use .slice():
var output = "[" + input.slice(1,-1) + "]";
For what it's worth, to replace both ( and ) use:
str = "(boob)";
str = str.replace(/[\(\)]/g, ""); // yields "boob"
regex character meanings:
[ = start a group of characters to look for
\( = escape the opening parenthesis
\) = escape the closing parenthesis
] = close the group
g = global (replace all that are found)
Edit
Actually, the two escape characters are redundant and eslint will warn you with:
Unnecessary escape character: ) no-useless-escape
The correct form is:
str.replace(/[()]/g, "")
var s ="(53.5595313, 10.009969899999987)";
s.replace(/\((.*)\)/, "[$1]")
This Javascript should do the job as well as the answer by 'nnnnnn' above
stringObject = stringObject.replace('(', '[').replace(')', ']')
If you need not only one bracket pair but several bracket replacements, you can use this regex:
var input = "(53.5, 10.009) more stuff then (12) then (abc, 234)";
var output = input.replace(/\((.+?)\)/g, "[$1]");
console.log(output);
[53.5, 10.009] more stuff then [12] then [abc, 234]
where do we start if we want to remove the affix from this sentence meangan menangkan dimenangkan
affix_list = [
'me-an',
'me-kan,
'di-kan
]
string = 'meangan menangkan dimenangkan'
so it will output
output = [
'ang',
'nang'
'menang'
]
You might want to use regular expressions for those replacements. Starting from your affix_list, this should work:
output = affix_list.reduce(function(str, affix) {
var parts = affix.split("-");
var regex = new RegExp("\\b"+parts[0]+"(\\S+)"+parts[1]+"\\b", "g");
return str.replace(regex, "$1")
}, string).split(" ");
Your regexes will look like this:
/\bme(\S+)an\b/g
/\bme(\S+)kan\b/g
/\bdi(\S+)kan\b/g
But note that you will of course need to replace me-kan before me-an, else "menangkan" will become nangk before the me-kan expression can be applied.
You'll need to start with Javascript regular expressions and iterate through the values, retrieving the middle value accordingly. I'll do that first one for you, and you can try out the rest :)
var re = /me(\w+)an/;
var str = "meangan";
var newstr = str.replace(re, "$1");
console.log(newstr);
// outputs ang
Reference: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions
In javascript, how do I remove all special characters from the string except the semi-colon?
sample string: ABC/D A.b.c.;Qwerty
should return: ABCDAbc;Qwerty
You can use a regex that removes anything that isn't an alpha character or a semicolon like this /[^A-Za-z;]/g.
const str = "ABC/D A.b.c.;Qwerty";
const result = str.replace(/[^A-Za-z;]/g, "");
console.log(result);
var str = "ABC/D A.b.c.;Qwerty";
var result = str.replace(/[^A-Za-z;]/g, ""); // 21ABCDAbc;Qwerty
Live DEMO
I have the following string: pass[1][2011-08-21][total_passes]
How would I extract the items between the square brackets into an array? I tried
match(/\[(.*?)\]/);
var s = 'pass[1][2011-08-21][total_passes]';
var result = s.match(/\[(.*?)\]/);
console.log(result);
but this only returns [1].
Not sure how to do this.. Thanks in advance.
You are almost there, you just need a global match (note the /g flag):
match(/\[(.*?)\]/g);
Example: http://jsfiddle.net/kobi/Rbdj4/
If you want something that only captures the group (from MDN):
var s = "pass[1][2011-08-21][total_passes]";
var matches = [];
var pattern = /\[(.*?)\]/g;
var match;
while ((match = pattern.exec(s)) != null)
{
matches.push(match[1]);
}
Example: http://jsfiddle.net/kobi/6a7XN/
Another option (which I usually prefer), is abusing the replace callback:
var matches = [];
s.replace(/\[(.*?)\]/g, function(g0,g1){matches.push(g1);})
Example: http://jsfiddle.net/kobi/6CEzP/
var s = 'pass[1][2011-08-21][total_passes]';
r = s.match(/\[([^\]]*)\]/g);
r ; //# => [ '[1]', '[2011-08-21]', '[total_passes]' ]
example proving the edge case of unbalanced [];
var s = 'pass[1]]][2011-08-21][total_passes]';
r = s.match(/\[([^\]]*)\]/g);
r; //# => [ '[1]', '[2011-08-21]', '[total_passes]' ]
add the global flag to your regex , and iterate the array returned .
match(/\[(.*?)\]/g)
I'm not sure if you can get this directly into an array. But the following code should work to find all occurences and then process them:
var string = "pass[1][2011-08-21][total_passes]";
var regex = /\[([^\]]*)\]/g;
while (match = regex.exec(string)) {
alert(match[1]);
}
Please note: i really think you need the character class [^\]] here. Otherwise in my test the expression would match the hole string because ] is also matches by .*.
'pass[1][2011-08-21][total_passes]'.match(/\[.+?\]/g); // ["[1]","[2011-08-21]","[total_passes]"]
Explanation
\[ # match the opening [
Note: \ before [ tells that do NOT consider as a grouping symbol.
.+? # Accept one or more character but NOT greedy
\] # match the closing ] and again do NOT consider as a grouping symbol
/g # do NOT stop after the first match. Do it for the whole input string.
You can play with other combinations of the regular expression
https://regex101.com/r/IYDkNi/1
[C#]
string str1 = " pass[1][2011-08-21][total_passes]";
string matching = #"\[(.*?)\]";
Regex reg = new Regex(matching);
MatchCollection matches = reg.Matches(str1);
you can use foreach for matched strings.