I have to split a string till a word in JavaScript.
var urlCurrent = "www.example.com/web/americas/home";
var siteNames = ["americas","international","europe","asia-pacific","africa-middle-east","russia","india"];
var siteNamesJoin = siteNames.join('|');
var siteUrlCurrent = urlCurrent.split(siteNamesJoin);
Here, I have to split the urlCurrent string using the words in the array. so that at the end I have to get www.example.com/web/americas. I am not getting the regex for that.
You may use
new RegExp("^.*?(?:" + siteNamesJoin + "|$)")
The pattern will look like
^.*?(?:americas|international|europe|asia-pacific|africa-middle-east|russia|india|$)
See the regex graph:
Details
^ - start of string
.*? - any 0 or more chars other than line break chars, as few as possible
(?:americas|international|europe|asia-pacific|africa-middle-east|russia|india|$) - any of the values in between pipes or end of string.
See JS demo:
var urlCurrent = "www.example.com/web/americas/home";
var siteNames = ["americas","international","europe","asia-pacific","africa-middle-east","russia","india"];
var siteNamesJoin = siteNames.join('|');
var match = urlCurrent.match(new RegExp("^.*?(?:" + siteNamesJoin + "|$)"));
var siteUrlCurrent = match ? match[0] : "";
console.log(siteUrlCurrent);
NOTE: if the siteNames "words" may contains special regex metacharacters, you will need to escape the siteNames items:
var siteNamesJoin = siteNames.map(function (x) { return x.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') }).join('|');
Also, if those words must match in between / or end of string, you may adjust the pattern:
var match = urlCurrent.match(new RegExp("^(.*?)/(?:(?:" + siteNamesJoin + ")(?![^/])|$)"));
See another demo.
var urlCurrent = "www.example.com/web/americas/home";
var siteNames = ["americas","international","europe","asia-pacific","africa-middle-east","russia","india"];
var siteNamesJoin = siteNames.map(function (x) { return x.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') }).join('|');
var match = urlCurrent.match(new RegExp("^.*?/(?:(?:" + siteNamesJoin + ")(?![^/])|$)"));
var siteUrlCurrent = match ? match[0] : "";
console.log(siteUrlCurrent);
using regex & match
var urlCurrent = "www.example.com/web/americas/home";
var siteNames = ["americas","international","europe","asia-pacific","africa-middle-east","russia","india"];
var regex = new RegExp(siteNames.join('|'));
var match = urlCurrent.match(regex);
if(match) {
var url = urlCurrent.substr(0, match[0].length + match.index)
console.log(url);
} else {
//invalid url
}
Maybe this helps you, i guess you don´t need a regex:
var urlCurrent = "www.example.com/web/americas/home";
var siteNames = ["americas","international","europe","asia-pacific","africa-middle-east","russia","india"];
var siteNamesJoin = siteNames.join('|');
var siteUrlCurrent = urlCurrent.split(siteNamesJoin);
const pos = urlCurrent.split("/")[2];
let url;
for(const index in siteNames) {
if(pos === siteNames[index]) {
url = urlCurrent.substring(0,urlCurrent.lastIndexOf("/"));
}
}
console.log(url);
Related
Edit
sorry if the question wasn't clear
here is the question..
create your version of javascript split function,
you may use indexOf and substring to help.
so if i give you a string "heellloolllloolllo" and i want to remove "llll" the function should return "heellloooolllo"
This what I did so far:
function split() {
var entered_string = document.forms["form1"]["str"].value;
var deleted_char = document.forms["form1"]["char"].value;
var index = entered_string.indexOf(deleted_char);
var i = deleted_char.length;
var result;
var x ;
for (x = 0; x< entered_string.length; x++ )
{
if (index < 0) {
result = entered_string;
} else {
result = entered_string.substring(0, index) +entered_string.substring(index+i);
}
}
alert(result)
}
Use the replace() function with the g at the end of your regular expression. It's called a "global modifier".
var string = 'heellloolllloolllo';
var res = string.replace(/llll/g, '');
console.log(res)
If your substring is a variable then you need to construct a new Regex object and set the g as the second parameter.
var string = 'heellloolllloolllo';
var find = 'llll';
var regex = new RegExp(find,'g');
var res = string.replace(regex, '');
console.log(res)
There are other useful modifiers you can use:
g - Global replace. Replace all instances of the matched string in the provided text.
i - Case insensitive replace. Replace all instances of the matched string, ignoring differences in case.
m - Multi-line replace. The regular expression should be tested for matches over multiple lines.
See this post for more information, credit to #codejoe.
Using String#replace and RegExp (the clean way)
var str = 'llllheellloolllloolllollll';
var matchStr = 'llll';
function removeSubString(str, matchStr) {
var re = new RegExp(matchStr, 'g');
return str.replace(re,"");
}
console.log(removeSubString(str, matchStr));
Using String#indexOf and String#substring
var str = 'llllheellloolllloolllollll';
var matchStr = 'llll';
function removeSubString(str, matchStr) {
var index = str.indexOf(matchStr);
while(index != -1) {
var firstSubStr = str.substring(0, index);
var lastSubStr = str.substring(index + matchStr.length);
str = firstSubStr + lastSubStr;
index = str.indexOf(matchStr);
}
return str;
}
console.log(removeSubString(str,matchStr))
I want to replace G$0 in the string gantt(G$0,$A4,$B4) with gantt(<>G$0<>,$A4,$B4). So I have the following code:
var str = '=gantt(G$0,$A4,$B4) ';
var val = "G$0";
var val2 = val.replace(/\$/, "\\$")
var reg = new RegExp(val2, 'g');
var str = str.replace(reg, '<>' + val + '<>');
The result in IE is: =gantt(<>GG$0<>,$A4,$B4) (note the GG). The problem seems to be IE10 specific.
Why is this happening, is this an IE bug?
The replace should assume a string could contain multiple instances of **G$0**.
There's no need to use RegEx at all. Stick to regular string replacement, and you won't have to escape the val string.
var str = '=gantt(G$0,$A4,$B4) ';
var val = "G$0";
var result = str.replace(val, '<>' + val + '<>');
If you want to replace multiple instances of val this can be done with .split and .join:
var str = '=gantt(G$0,$A4,$B4,G$0) ';
var val = "G$0";
var result = str.split(val).join('<>' + val + '<>');
I have the following bit of code :
var stringToMatch = "De$mond. \(blah)";
var pattern = "^" + stringToMatch;
var regex = new RegExp(pattern, "i");
return regex.test("testing De$mond.");
Now I need to escape stringToMatch before using it in pattern
A solution I found here suggest this method if I understand correctly :
var stringToMatch = "De$mond. \(blah)";
stringToMatch = stringToMatch.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
var pattern = "^" + stringToMatch;
var regex = new RegExp(pattern, "i");
return regex.test("testing De$mond.");
Why can't I simply escape all of the characters in stringToMatch instead?
e.g.
var stringToMatch = "De$mond. \(blah)";
var stringToMatchAsArrayOfChars = [];
for (var i = 0; i < stringToMatch.length; i++)
{
stringToMatchAsArrayOfChars.push(stringToMatch.substr(i, 1));
}
var stringToMatchEscaped = "";
for (var i = 0; i < stringToMatchAsArrayOfChars.length; i++)
{
if (stringToMatchAsArrayOfChars[i] !== " ")
{
stringToMatchEscaped = stringToMatchEscaped + "\\" + stringToMatchAsArrayOfChars[i];
}
else
{
stringToMatchEscaped = stringToMatchEscaped + " ";
}
}
var pattern = "^" + stringToMatch;
var regex = new RegExp(pattern, "i");
return regex.test("testing De$mond.");
I understand that the above method is much more verbose but what it basically does is :
var stringToMatchEscaped = "\D\e\$\m\o\n\d\. \\\(\b\l\a\h\)";
But it's not working. Why is that?
And, also, is there some other way of escaping stringToMatch other than the one suggested in the link I provided? i.e. without specifying which characters to escape like it's being dones with /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g ?
here is a simple regexp to make safe regexp patterns from string input.
var pattern= "De$mond.";
var regex = new RegExp(pattern.replace( /([.*+?^${}()|[\]\/\\])/g , '\\$1'), "i");
regex.test("testing De$mond. string here."); // === true
note that this means you cannot use the "wildcards" or any RegExp syntax, but you'll end up with a real regexp that will perform a literal match from the source text to the pattern.
So I'm using regex to grab information from a string, the issue is I need to both start up and stop at a / in the string.
Here's an example
var regexp = /\/(.*?)=(.*?)\//g;
var url_hash = "/s=lorem+ipsum/p=2/";
var match;
var result = {};
while ((match = regexp.exec(url_hash)) != null) {
result[match[1]] = match[2];
}
I can grab result['s'] without issue, but grabbing result['p'] becomes problematic, because the ending / for result['s'] is the same as the starting / for result['p']. If I changed the string to /s=lorem+ipsum//p=2/ it works perfectly, but of course that's hideous. So how can I fix this so that it both ends and starts up at the /? I'm stuck, any help is appreciated.
Use this regex:
/\/([^/=]+)=([^/]+)/
Code:
var regexp = /\/([^/=]+)=([^/]+)/g;
var url_hash = "/#!/s=lorem+ipsum/p=2/";
var match;
var result = {};
while ((match = regexp.exec(url_hash)) != null) {
result[match[1]] = match[2];
document.writeln(match[1] + ' = ' + match[2] + '<br>');
}
OUTPUT:
s = lorem+ipsum
p = 2
Online demo of the code
Why can't you just split it?
var result = {};
var url = "/#!/s=lorem+ipsum/p=2/".slice(4, -1).split('/');
for (i in url) {
var value = url[i].split('=');
result[value[0]] = value[1];
}
console.log(result);
You can determine the look-ahead set for part after the = yourself instead of adding it to the regular expression. The look-ahead set is "everything but a forward slash".
var regexp = /\/(\w+)=([^/]+)/g;
Btw, I'm assuming that the part before the = is word-like (i.e. alphanumeric)
I have the following string in JavaScript
var mystring = " abcdef(p,q); check(x,y); cef(m,n);"
I would want to do a string replace such that my final string is :
mystring = " abcdef(p,q); someothercheck\(x,y\); cef(m,n);"
x and y should remain same after the substitution. and the backslashes are necessary since I need to pass them to some other command.
There can be other Parantheses in the string too.
If you don't have other parenthesis, it should be easy.
mystring = mystring.replace("check(", "someothercheck\\(");
mystring = mystring.replace(")", "\\)");
EDIT This works also in the case of multiple parenthesis (It does not affect the empty ones).
var str=" abcdef; check(x,y); cef();"
patt = /((\w)/g;
// transform (x in \(x
str = str.replace(patt, '\\($1');
patt = /(\w)\)/g
// transform y) in y\);
str = str.replace(patt, '$1\\)');
// transform check in someothercheck
str = str.replace('check', 'someothercheck');
EDIT Now it converts only the check strings.
function convertCheck(str, check, someOtherCheck) {
// console.log(str + " contains " + check + "? ");
// console.log(str.indexOf(check));
if (str.indexOf(check) === -1) return str;
var patt1 = /\((\w)/g,
patt2 = /(\w)\)/g;
str = str.replace(patt1, '\\($1');
str = str.replace(patt2, '$1\\)');
str = str.replace(check, someOtherCheck);
return str;
}
var str = "abcdef(); check(x,y); cef();",
tokens = str.split(';');
for (var i = 0; i < tokens.length; i++) {
tokens[i] = convertCheck(tokens[i], "check", "someothercheck");
}
str = tokens.join(";");
alert(str); // "abcdef(); someothercheck/(x,y/); cef();"
var myString = "abcdef; check(x,y); cef;";
myString.replace(/(\w+)\(/, 'someother$1(')
.replace(/\(/g, '\\(')
.replace(/\)/g, '\\)')