i am unable to get specific regex [duplicate] - javascript

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 5 years ago.
aa=&bb=0108135719&cc=20180108135935&dd=ee&ff=201801081358544265&gg=1&hh=1000&ii=&
i have a string like this and i trying to get specific value from it, it is in text format. what can i do if i want to get value of 'aa' it null and value of 'bb' it is 0108135719 tats it. I'm tried different regex but unable to get the desired output.

You can do something like
var t="aa=&bb=0108135719&cc=20180108135935&dd=ee&ff=201801081358544265&gg=1&hh=1000&ii=&".split("&")
console.log(t)
var ansObj = {}
t.forEach((element) => {
const elementArray = element.split("=")
const key = elementArray[0]
const value = elementArray[1]
ansObj[key] = value
})
console.log(ansObj)

Related

Required a solution in searching string in NodeJs [duplicate]

This question already has answers here:
Use dynamic (variable) string as regex pattern in JavaScript
(8 answers)
Closed 5 years ago.
I have a string "platform:17.01.02" and I want to search for "17.01.02" which I'm giving through a variable.
a = "platform:17.01.02"
b = "17.01.02" (has to taken from user)
a.search(/b/)
The above statement search for "b" and not for variable value "b". Can any one help how can i search that?
Try this:
var a = "platform:17.01.02";
var b = "17.01.02";
console.log(a.search(new RegExp(b))); //9
If you just want to know if a contains b, use:
a.indexOf(b) >= 0

`alert` of `match` result shows comma-separated string; how to get only the second part? [duplicate]

This question already has answers here:
Regex to extract substring, returning 2 results for some reason
(5 answers)
Closed 1 year ago.
When parsing this
<title>I have a cat</title> <div class="chat_message_wrapper chat_message_right">
with
var re_title = new RegExp("<title>(.*?)</title>");
var content = xmlhttp.responseText;
// var title = re_title.exec(content);
var title = content.match(re_title);
// title = strip(title);
alert(title);
for some reason, I get this in an alert:
<title>I have a cat</title>,I have a cat
How can I only get the “I have a cat” part?
The match method returns an array with first the complete match, and in the other elements the capture groups. Since you have one capture group, you need to get the value from the array at index 1:
title[1]

Jquery/JS - write url params in array [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I have an unknown number of params in the URL. I want to write those in an Array, which i can use to refin a search.
But i don't know how. I found a big amount of scripts that work with just one Parameter. The idea is to work with for each, or something like this.
Example Url:
https://exampleurl.com&func=ll&nexturl=&promting=done&inputLabel1=(OTSubType:0 or OTSubType:144)&DSmaxrows=20&&folds=checked
Thanks for your help!
Greetings, gefler
function getParams(url){
var params = [];
url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
params.push({ key: key, value: value });
});
return params;
}
var yourUrl = "https://exampleurl.com&func=ll&nexturl=&promting=done&inputLabel1=(OTSubType:0 or OTSubType:144)&DSmaxrows=20&&folds=checked";
console.log(getParams(yourUrl))

Getting the first item in an [word, word] [duplicate]

This question already has answers here:
How to get the first element of an array?
(35 answers)
Closed 8 years ago.
I am trying to get the first word out of the variable var solution = [cow, pig]
I have tried everything from strings to arrays and I can't get it. Please help.
As per the comments
solution[0]
Will return the first item in the array.
solution[1]
would be the second, or undefined if the array was:
var solution = [cow]
Is solution an array, or is it in that form? (var solution = [cow, pig]) You also need to add quotes around those values, unless those values are defined variables.
You need to change the variable to look like this:
var solution = ['cow', 'pig']
If so, just get the value at subscript 0.
var result = solution[0];
console.log(result);
If you mean an string like
solution = "cow pig".
Do
solution = solution.split(' ')[0];
console.log(solution); //Will return cow

Get the last occurrence of string within another string [duplicate]

This question already has answers here:
endsWith in JavaScript
(30 answers)
Closed 9 years ago.
Is there a javascript function to get the last occurrence of a substring in a String
Like:
var url = "/home/doc/some-user-project/project";
I would like a function that returns true if the String contains project at his end.
I know str.indexOf() or str.lastIndexOf() but is there another function that do the job or should I do it?
Thanks for the answer
Something like
var check = "project",
url = "/home/doc/some-user-project/project";
if (url.substr(-check.length) == check){
// it ends with it..
}
Try this
<script>
var url = "/home/doc/some-user-project/project";
url.match(/project$/);
</script>
The response is a array with project, if the responde is 'null' because it is not found

Categories