This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 7 years ago.
I know that js substring method accept number parameters but what if I need to get a part of string beginning with ?. So the method must accept a char value for beginning position. How can I do this?
http://localhost:8080/new_prog_24/search.action?country=&city=&sex=1&beginage=16&endage=65&children=&confession=0#2
You could use the indexOf method so that you know where is the "?" char.
var test = "is this a test? Yes it is!";
var startAt = test.indexOf('?') + 1; // + 1 so that '?' is not returned
test.substring(startAt);
Something like this should get you the results (unless I am understanding your question incorrectly, apologies if I did):-
index = myString.indexOf("?");
var mysubString = "";
if(index == 0) //<--- only for strings that are beginning with a "?"
{
mysubString = myString.Substring(1, myString.length);
}
this should get you a string that starts with a "?" but without displaying the "?" as part of your substring...until the end of the string.
Related
This question already has answers here:
How to tell if a string contains a certain character in JavaScript?
(21 answers)
Closed 4 years ago.
I'm a beginner in node.js so please do excuse me if my question is foolish. As we know we can use
var regex = /[ !##$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/g;
regex.test(str);
to check whether a string contains special charecters or not .But what I'm asking is how to check for only a particular charecter means how can I check only presence of #.
I tried to do
var regex = /[#]/g; regex.test(str).
Although it's not working but are there any other method of doing this?
You don't need a regex to find a single character in a string. You can use indexOf, like this:
var hasHashtag = str.indexOf('#') >= 0;
This returns true if the character is in the string.
Use includes to check the existence of # in your string. You don't actually require regex to do that.
var str = 'someSt#ring';
var res = str.includes('#');
console.log(res);
str = 'someSt#ri#ng';
res = str.includes('#');
console.log(res);
str = 'someString';
res = str.includes('#');
console.log(res);
Use indexOf
str.indexOf('#') >= 0;
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 5 years ago.
If anyone could help me with me this, it would very appreciative.
I'm trying to extract the string that comes after the last question mark ('rrrrrrrrrrrrrrrrrrrr' in the following example):
from:
https://www.youtube.com/embed/url=https://sites.google.com/a/yink.net/uok/ee.xml&container=enterprise&view=default&lang=en&country=ALL&sanitize=0&v=9a2d7691ff90daec&libs=core&mid=38&parent=http://j.yink.net/home/sandbox?rrrrrrrrrrrrrrrrrrrr
to:
rrrrrrrrrrrrrrrrrrrr
I've tried this code, but it doesn't give the right result:
document.getElementById("demo").innerHTML = window.location.search.slice(1);
var url = window.location.href will give you the URL
Then you'll want to grab everything after the ? using
var queryString = url ? url.split('?')[1] : window.location.search.slice(1);
I pieced the answer together from: https://www.sitepoint.com/get-url-parameters-with-javascript/
Edit: -------------------------------------
To get the text after the last question mark then you can use:
url.split('?').pop() instead of url.split('?')[1]
(Credit to: #I wrestled a bear once )
Use lastIndexOf to get the last index of the "?" and then use substr to get the substring.
Make sure you sanitize the string and make sure there is a "?" in it first or else it will throw an error.
let str = `https://www.youtube.com/embed/url=https://sites.google.com/a/yink.net/uok/ee.xml&container=enterprise&view=default&lang=en&country=ALL&sanitize=0&v=9a2d7691ff90daec&libs=core&mid=38&parent=http://j.yink.net/home/sandbox?rrrrrrrrrrrrrrrrrrrr`;
console.log(str.substr(str.lastIndexOf("?")+1));
This question already has answers here:
How to grab substring before a specified character in JavaScript?
(12 answers)
Closed 6 years ago.
I am attempting to take the city out of a city, state string. For instance, "Portland, OR" would need to equal "Portland". How can I use Javascript or a regular expression to do this?
var myString = "Portland, OR";
I want to extract everything up to the comma but not including the comma.
var city = "Portland, OR".split(",")[0];
With regex:
var regex = /^[^,]+/;
var city = regex.exec("Portland, OR");
This is the regex version
var result = myString.match(/^[^,]+/)
UPDATE
This one always returns a value without error
var result = String(myString || "").match(/^[^,]*/)[0]
This question already has answers here:
Why isn't this split in javascript working?
(2 answers)
Closed 8 years ago.
please, could you help me with my task: I need to replace part of string and probably the best way is regular expression but I don't know, how to make it working. I want to do this:
http://someweb.com/section/&limit=10&page=2
replace page=2 with page=3 so string will be:
http://someweb.com/section/&limit=10&page=3
I tried to do something like this:
// set string in t variable
t.replace('/page=[0-9]/', 'page=$1++') });
Thank you very much for your help :)
In our case first argument should be regexp, but in your variant this is string '/page=[0-9]/' (remove '). In replace you can pass function as second argument, and do with matched data what you want. (for example add +1 to page=)
var str = "http://someweb.com/section/&limit=10&page=2";
str.replace(/page=(\d+)/, function (match, page) {
return 'page=' + (+page + 1); // plus before page converts string to number
});
Example
You can also try below code.
var url = "http://someweb.com/section/&limit=10&page=2",
reExp = /page=([0-9])+/,
result = reExp.exec(url);
url = url.replace(reExp, 'page=' + (+result[1] + 1));
console.log(url)
This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Closed 9 years ago.
I'm sure this was supposed to work, but I can't get it doing what I want it to:
new_str = old_str.replace(3, "a");
// replace index 3 (4th character) with the letter "a"
So if I had abcdef then above should return abcaef but I must have gotten something wrong. It is changing characters, but not the expected ones.
Either native JS or jQuery solution is fine, whatever is best (I'm using jQuery on that page).
I've tried searching but all tutorials talk of Regex, etc. and not the index replace thing.
You appear to want array-style replacement, so convert the string into an array:
// Split string into an array
var str = "abcdef".split("");
// Replace char at index
str[3] = "a";
// Output new string
console.log( str.join("") );
Here are three other methods-
var old_str= "abcdef",
//1.
new_str1= old_str.substring(0, 3)+'a'+old_str.substring(4),
//2.
new_str2= old_str.replace(/^(.{3}).(.*)$/, '$1a$2'),
//3.
new_str3= old_str.split('');
new_str3.splice(3, 1, 'a');
//return values
new_str1+'\n'+new_str2+'\n'+ new_str3.join('');
abcaef
abcaef
abcaef