Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I need to extract a part of a string. My goal is to extract from this string:
217591953.1396968335.2.2.utmcsr=search_email_alerts|utmccn=(not set)|utmcmd=email
the content between utmcsr= and |
So the output would be : "search_email_alerts".
var str = "217591953.1396968335.2.2.utmcsr=search_email_alerts|utmccn=(not set)|utmcmd=email";
var newStr1 = str.split("utmcsr=");
var newStr2 = newStr1[1].split("|");
var extractedStr = newStr2[0];
The variable extractedStr will be "search_email_alerts".
Use regular expression like following:
var test = "217591953.1396968335.2.2.utmcsr=search_email_alerts|utmccn=(not set)|utmcmd=email";
var result = test.match("/utmcsr=([^|]*)\|/");
alert(result[1]); // result[1] is your desired string
var str = "217591953.1396968335.2.2.utmcsr=search_email_alerts|utmccn=(not set)|utmcmd=email";
match = str.match(/utmcsr=(.*?)\|/);
output = match[1];
var str = "217591953.1396968335.2.2.utmcsr=search_email_alerts|utmccn=(not set)|utmcmd=email"
var result = str.substring(str.indexOf("utmcsr=") + 7,str.indexOf("|"))
Result contains the desired text.
You can do that in two ways
substring
match
Here's how you can do it with regex:
var str= "217591953.1396968335.2.2.utmcsr=search_email_alerts|utmccn=(not set)|utmcmd=email";
var res = str.match(/^.*utmcsr=(.*)\|.*\|.*$/);
res[1]
Here you go -
var str = "217591953.1396968335.2.2.utmcsr=search_email_alerts|utmccn=(not set)|utmcmd=email";
var startIndex = str.indexOf("utmcsr=") + "utmcsr=".length;
var numOfCharsToExtract = str.indexOf("|") - startIndex;
var result = str.substring(startIndex, numOfCharsToExtract);
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
How to remove word before slash using jquery?
/bis/admin
In my case, I want to remove "bis". any help?
You can use the JavaScript split method
var text = "/bis/admin";
var result = text.split("/");
console.log(result[result.length - 1]);
Another example
var text = "/bis/admin/dashboard";
var result = text.split("/");
console.log(result[result.length - 1]);
You could do the following using a regular expression
var el = '/bis/admin';
var newEl = el.replace(/^.*\//, "");
newEl will be 'admin'
You can just do:
let myString = '/bis/admin';
myString.split('/')[myString.split('/').length-1];
var str = "/bis/admin";
var splitStr = str.split("/");
var out = "/" + n[n.length - 1] //Out contains /admin
split string with /.
var strn = "/bis/admin";
console.log(strn.split("/")[2]);
Or you can simply use regex
var strn = "/bis/admin";
console.log(strn.replace(/^\/([^/]*)\//,""));
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm trying to extract the last segment of URLs like this:
localhost:15043/Maintenance/ModelDetails/3?makeId=14
and this:
//localhost:15043/Maintenance/ModelDetails/3
I used below code, but this code does not work in this link:
localhost:15043/Maintenance/ModelDetails/3?makeId=14
var last_segment = window.location.hash.split('/').pop();
You can use the pathname, search, and/or hash part of the window.location to do this. Ignore the parser here as it is just used as an example to run this code.
var url = "http://localhost:15043/Maintenance/ModelDetails/3?blah=abc&makeId=14#test";
function parseUrl(url) {
var a = document.createElement("a");
a.href = url;
return a;
}
var parser = parseUrl(url);
var last_segment = parser.pathname.split('/').pop();
var searchParams = parser.search.substring(1).split("&");
var lastParamValue = searchParams[searchParams.length-1].split("=")[1];
var hash = parser.hash;
console.log(last_segment);
console.log(lastParamValue);
console.log(hash);
Won't this simple regex .+\/ work for you with replace() ?
var getLastSegment = function(url) {
return url.replace(/.+\//, '');
};
console.log(getLastSegment("localhost:15043/Maintenance/ModelDetails/3?makeId=14"));
console.log(getLastSegment("//localhost:15043/Maintenance/ModelDetails/3"));
You can use a regexp in your .split() method.
const pattern1 = /\?|\/|\&/;
const pattern2 = /\//;
const str = 'localhost:15043/Maintenance/ModelDetails/3?makeId=14';
console.log(str.split(pattern1).pop()); // return 'makeId=14'
console.log(str.split(pattern2).pop()); // return '3?makeId=14'
The pattern1 will split the url on the ?, / and & characters, and pattern2 only on / character. So you can adjust the regexp to match with what you want.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I have this code:
var str = 'country/city/area'
var idx = str.lastIndexOf('country/city')
// idx = 0
And idx is always 0. Shouldn't idx be 12? My goal is to use it substr() in order to take the string 'area' out of the str.
var str = 'country/city/area'
var pattern = 'country/city/'
var idx = str.lastIndexOf(pattern) + pattern.length
var substring = str.substring(idx, str.length)
Explanation
1) Define the pattern you are searching for
2) Find the beginning of the pattern and add the length of the pattern => now you are at the end
3) Copy the part behind the pattern to the end of the string
if you want to get the last word, you can search for the last forward slash and get everything after it:
str.substr(str.lastIndexOf('/') + 1)
if you want to get everything after 'country/city/' but for example you don't know if this the first part of the string, you can use
str.substr(str.indexOf('country/city/') + 13);
it's not 100% clear from your question, what exactly you are trying to achieve though.
You're going to want to add the length of the string that you search for:
var str = 'country/city/area';
var checkStr = 'country/city';
var idx = str.lastIndexOf(checkStr);
var lastCharIndex = idx + checkStr.length;
// idx = 0
// idx = 12
note - it would be 12, not 13, because you didn't include the final "/" in your lastIndexOf parameter.
May be you can achieve your goal as follows;
var str = 'country/city/area',
newStr = str.replace("/area","");
console.log(newStr);
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have a requirement to remove last n characters from string or remove 'page' from a particular string.
Eg:
var string = 'facebookpage';
Expected output string = 'facebook'
I would like to remove 'page' from the string.
Done it using substring.
var str = "facebookpage";
str = str.substring(0, str.length - 4);
Could you help me to find some better way to do it.
Regex for this:
//str - string;
//n - count of symbols, for return
function(str, n){
var re = new RegExp(".{" + n + "}","i");
return str.match(re);
};
EDIT:
For remove last n characters:
var re = new RegExp(".{" + n + "}$","i");
return str.replace(re, "");
UPDATE:
But use regex for this task, not good way; For example, AVG Runtime for 100000 iterations:
Str length solution = 63.34 ms
Regex solution = 172.2 ms
Use javascript replace function
var str = "facebookpage";
str = str.replace('page','');
You can use this regular expression :
(.*)\\w{4}
code :
var regex =(new RegExp("(.*)\\w{4}"))
val output = regex .exec("facebookpage")
// output is : ["facebookpage", "facebook"]
// output[1] is the Expected output which you want.
Hope this helps.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am looking for a way to count certain words on a string.
$string = "How are you doing today? You are such a nice person!";
My goal is to count how many "are" in the $string variable.
Desire result: 2
Try this:
var count = yourString.match(/\bare\b/g);
count = count? count.length : 0; //checking if there are matches or not.
console.log(count);
var string = "are you bare footed?";
console.log(string.split(/\bare\b/).length - 1);
var str = "How are you doing today? You are such a nice person!";
var matchs;
matchs = str.match(/are/gi);
alert(matchs.length);
try this javascript match it returns match string as array format, and length method returns how many array index match.
try below.
var temp = "How are you doing today? You are such a nice person!.";
var count = countOcurrences(temp,"are");
alert(count);
function countOcurrences(str, value){
var regExp = new RegExp(value, "gi");
return str.match(regExp) ? str.match(regExp).length : 0;
}
Try this:
$string = "How are you doing today? You are such a nice person!";
var count = $string.match(/are/g);
alert(count.length);
Try this:
var string = "How are you doing today? You are such a nice person!";
var num = string.match(/are/g).length;
alert(num);
Here is Demo
You can also try something like this:
var temp = "How are you doing today? You are such a nice person!.";
var arr = [];
for(var i = 0;i< temp.split(' ').length; i++)
{
if(temp.split(' ')[i] === "are"){
arr.push(temp.split(' '));
}
}
alert(arr.length)