Javascript get the string between two symbols [duplicate] - javascript

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I have the following string and I'm trying to retrieve the string between two symbols
http://mytestdomain.com/temp-param-page-2/?wpv_paged_preload_reach=1&wpv_view_count=1&wpv_post_id=720960&wpv_post_search&wpv-women-clothing[]=coats
I need to retrieve wpv-women-clothing[] or any other string between the last & and the last = in the URL
Should I use regex for this or is there a function in Javascript/jQuery already well suited for this?
Thanks

var str = "http://mytestdomain.com/temp-param-page-2/?wpv_paged_preload_reach=1&wpv_view_count=1&wpv_post_id=720960&wpv_post_search&wpv-women-clothing[]=coats";
var last =str.split('&').pop().split('=')
console.log(last[0]) // wpv-women-clothing[]
jsFiddle example
Split the string on the ampersands (.split('&')), take the last one (.pop()), then split again on the = (.split('=')) and use the first result last[0].

.*&(.*?)=.*
This should do it.
See demo.
http://regex101.com/r/lZ5bT3/1

Group index 1 contains your desired output,
\&([^=]*)(?==[^&=]*$)
DEMO
> var re = /\&([^=]*)(?==[^&=]*$)/g;
undefined
> while ((m = re.exec(str)) != null) {
... console.log(m[1]);
... }
wpv_post_search&wpv-women-clothing[]

Can you try:
var String = "some text";
String = $("<div />").html(String).text();
$("#TheDiv").append(String);

Related

Split text with {{Text}} format Javascript [duplicate]

This question already has answers here:
Regex to get string between curly braces
(16 answers)
Closed 4 years ago.
Sorry to bother you all. I'm no idea about regular expression. But right now I need one very badly.
I want to split text using this format {{Text}}. The "Text" can be anything. All I need is split the text at the position of {{Text}}.
Here is a sample.
var Regx = My Regx;
var String = "{{This}} is a {{test}} string to be {{spliced}} with {{Regular}} Expression";
var SplitArray = String.split(Regx);
// it will give me an array like this
// ["","is a ","string to be "," with"," Expression"]
Thank you in advance.
Edit:
I solved it myself too. It is {{[^{}]+}}
You can do this way
var test = "{{This}} is a {{test}} string to be {{spliced}} with {{Regular}} Expression";
var SplitArray = test.split(/\{\{.*?\}\}/);
console.log(SplitArray)
Try this:
var Regx = /\{\{.*?\}\}/;
var String = "{{This}} is a {{test}} string to be {{spliced}} with {{Regular}} Expression";
var SplitArray = String.split(Regx);
console.log(SplitArray);
// it will give me an array like this
// ["","is a ","string to be "," with"," Expression"]

RegEx extract all real numbers from a string [duplicate]

This question already has answers here:
Regex exec only returning first match [duplicate]
(3 answers)
Closed 8 years ago.
This regex in JavaScript is returning only the first real number from a given string, where I expect an array of two, as I am using /g. Where is my mistake?
/[-+]?[0-9]*\.?[0-9]+/g.exec("-8.075090 -35.893450( descr)")
returns:
["-8.075090"]
Try this code:
var input = "-8.075090 -35.893450( descr)";
var ptrn = /[-+]?[0-9]*\.?[0-9]+/g;
var match;
while ((match = ptrn.exec(input)) != null) {
alert(match);
}
Demo
http://jsfiddle.net/kCm4z/
Discussion
The exec method only returns the first match. It must be called repeatedly until it returns null for gettting all matches.
Alternatively, the regex can be written like this:
/[-+]?\d*\.?\d+/g
String.prototype.match gives you all matches:
var r = /[-+]?[0-9]*\.?[0-9]+/g
var s = "-8.075090 -35.893450( descr)"
console.log(s.match(r))
//=> ["-8.075090", "-35.893450"]

How to trim a string to its last four characters? [duplicate]

This question already has answers here:
How to get the last character of a string?
(15 answers)
Closed 8 years ago.
I know there are methods to remove characters from the beginning and from the end of a string in Javascript. What I need is trim a string in such a way that only the last 4 characters remain.
For eg:
ELEPHANT -> HANT
1234567 -> 4567
String.prototype.slice will work
var str = "ELEPHANT";
console.log(str.slice(-4));
//=> HANT
For, numbers, you will have to convert to strings first
var str = (1234567).toString();
console.log(str.slice(-4));
//=> 4567
FYI .slice returns a new string, so if you want to update the value of str, you would have to
str = str.slice(-4);
Use substr method of javascript:
var str="Elephant";
var n=str.substr(-4);
alert(n);
You can use slice to do this
string.slice(start,end)
lets assume you use jQuery + javascript as well.
Lets have a label in the HTML page with id="lblTest".
<script type="text/javascript">
function myFunc() {
val lblTest = $("[id*=lblTest]");
if (lblTest) {
var str = lblTest.text();
// this is your needed functionality
alert(str.substring(str.length-4, str.length));
} else {
alert('does not exist');
}
}
</script>
Edit: so the core part is -
var str = "myString";
var output = str.substring(str.length-4, str.length);

Javascript Regular Expression multiple match [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 6 years ago.
I'm trying to use javascript to do a regular expression on a url (window.location.href) that has query string parameters and cannot figure out how to do it. In my case, there is a query string parameter can repeat itself; for example "quality", so here I'm trying to match "quality=" to get an array with the 4 values (tall, dark, green eyes, handsome):
http://www.acme.com/default.html?id=27&quality=tall&quality=dark&quality=green eyes&quality=handsome
You can use a regex to do this.
var qualityRegex = /(?:^|[&;])quality=([^&;]+)/g,
matches,
qualities = [];
while (matches = qualityRegex.exec(window.location.search)) {
qualities.push(decodeURIComponent(matches[1]));
}
jsFiddle.
The qualities will be in qualities.
A slight variation of #alex 's answer for those who want to be able to match non-predetermined parameter names in the url.
var getUrlValue = function(name, url) {
var valuesRegex = new RegExp('(?:^|[&;?])' + name + '=([^&;?]+)', 'g')
var matches;
var values = [];
while (matches = valuesRegex.exec(url)) {
values.push(decodeURIComponent(matches[1]));
}
return values;
}
var url = 'http://www.somedomain.com?id=12&names=bill&names=bob&names=sally';
// ["bill", "bob", "sally"]
var results = getUrlValue('names', url);
jsFiddle

splitting a string based on delimiter [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I split a string, breaking at a particular character?
I have a string in following format
part1/part2
/ is the delimiter
now I want to get split the string and get part 1. How can I do it?
result = "part1/part2".split('/')
result[0] = "part1"
result[1] = "part2
split the string and get part 1
'part1/part2'.split('/')[0]
var tokens = 'part1/part2'.split('/');
var delimeter = '/';
var string = 'part1/part2';
var splitted = string.split(delimeter);
alert(splitted[0]); //alert the part1
var result = YourString.split('/');
For your example result will be an array with 2 entries: "part1" and "part2"

Categories