How to parse the key and get its value in javascript
up vote
1
down vote
favorite
I am opening a URL from javascript. I need to look for the term "colour: x" and then retrieve the value x.
request.get("URL", function (error, res, body)
val = body.indexOf('colour') -> works
which means that web page has the string "colour".
Web page looks like this
size: 8 colour: 1
So, Here I need to retrieve the value of the key 'colour'.
javascript
New contributor
add a comment |
up vote
1
down vote
favorite
I am opening a URL from javascript. I need to look for the term "colour: x" and then retrieve the value x.
request.get("URL", function (error, res, body)
val = body.indexOf('colour') -> works
which means that web page has the string "colour".
Web page looks like this
size: 8 colour: 1
So, Here I need to retrieve the value of the key 'colour'.
javascript
New contributor
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am opening a URL from javascript. I need to look for the term "colour: x" and then retrieve the value x.
request.get("URL", function (error, res, body)
val = body.indexOf('colour') -> works
which means that web page has the string "colour".
Web page looks like this
size: 8 colour: 1
So, Here I need to retrieve the value of the key 'colour'.
javascript
New contributor
I am opening a URL from javascript. I need to look for the term "colour: x" and then retrieve the value x.
request.get("URL", function (error, res, body)
val = body.indexOf('colour') -> works
which means that web page has the string "colour".
Web page looks like this
size: 8 colour: 1
So, Here I need to retrieve the value of the key 'colour'.
javascript
javascript
New contributor
New contributor
edited 15 hours ago
Manohar Reddy Poreddy
4,2764144
4,2764144
New contributor
asked 16 hours ago
Tamilmani Natarajan
111
111
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
To search for a pattern in any general text:
You can use a regular expression if you know how your information is written.
This regular expression should do the job :
/bcolour:s+(d+)/
(the word "colour:" followed by any space, and then by any number of digits (d+
).
It captures the digits, so this will be the value of the first capture group (found[1]
) in my example.
body = `size: 8 colour: 1`
let regex = /bcolour:s+(d+)/;
let found = body.match(regex);
console.log(found[1]);
In the case there is no match (i.e., no 'colour: xx' in the page), the found
result will be null
, so you should of course check for it before, for safety.
body = `size: 8 but unfortunately, no colour here`
let regex = /bcolour:s+(d+)/;
let found = body.match(regex);
//console.log(found[1]); // Uncaught TypeError: Cannot read property '1' of null
// This snippet below is safe to use :
if (found) {
console.log(found[1]);
} else {
console.log('not found');
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
To search for a pattern in any general text:
You can use a regular expression if you know how your information is written.
This regular expression should do the job :
/bcolour:s+(d+)/
(the word "colour:" followed by any space, and then by any number of digits (d+
).
It captures the digits, so this will be the value of the first capture group (found[1]
) in my example.
body = `size: 8 colour: 1`
let regex = /bcolour:s+(d+)/;
let found = body.match(regex);
console.log(found[1]);
In the case there is no match (i.e., no 'colour: xx' in the page), the found
result will be null
, so you should of course check for it before, for safety.
body = `size: 8 but unfortunately, no colour here`
let regex = /bcolour:s+(d+)/;
let found = body.match(regex);
//console.log(found[1]); // Uncaught TypeError: Cannot read property '1' of null
// This snippet below is safe to use :
if (found) {
console.log(found[1]);
} else {
console.log('not found');
}
add a comment |
up vote
2
down vote
To search for a pattern in any general text:
You can use a regular expression if you know how your information is written.
This regular expression should do the job :
/bcolour:s+(d+)/
(the word "colour:" followed by any space, and then by any number of digits (d+
).
It captures the digits, so this will be the value of the first capture group (found[1]
) in my example.
body = `size: 8 colour: 1`
let regex = /bcolour:s+(d+)/;
let found = body.match(regex);
console.log(found[1]);
In the case there is no match (i.e., no 'colour: xx' in the page), the found
result will be null
, so you should of course check for it before, for safety.
body = `size: 8 but unfortunately, no colour here`
let regex = /bcolour:s+(d+)/;
let found = body.match(regex);
//console.log(found[1]); // Uncaught TypeError: Cannot read property '1' of null
// This snippet below is safe to use :
if (found) {
console.log(found[1]);
} else {
console.log('not found');
}
add a comment |
up vote
2
down vote
up vote
2
down vote
To search for a pattern in any general text:
You can use a regular expression if you know how your information is written.
This regular expression should do the job :
/bcolour:s+(d+)/
(the word "colour:" followed by any space, and then by any number of digits (d+
).
It captures the digits, so this will be the value of the first capture group (found[1]
) in my example.
body = `size: 8 colour: 1`
let regex = /bcolour:s+(d+)/;
let found = body.match(regex);
console.log(found[1]);
In the case there is no match (i.e., no 'colour: xx' in the page), the found
result will be null
, so you should of course check for it before, for safety.
body = `size: 8 but unfortunately, no colour here`
let regex = /bcolour:s+(d+)/;
let found = body.match(regex);
//console.log(found[1]); // Uncaught TypeError: Cannot read property '1' of null
// This snippet below is safe to use :
if (found) {
console.log(found[1]);
} else {
console.log('not found');
}
To search for a pattern in any general text:
You can use a regular expression if you know how your information is written.
This regular expression should do the job :
/bcolour:s+(d+)/
(the word "colour:" followed by any space, and then by any number of digits (d+
).
It captures the digits, so this will be the value of the first capture group (found[1]
) in my example.
body = `size: 8 colour: 1`
let regex = /bcolour:s+(d+)/;
let found = body.match(regex);
console.log(found[1]);
In the case there is no match (i.e., no 'colour: xx' in the page), the found
result will be null
, so you should of course check for it before, for safety.
body = `size: 8 but unfortunately, no colour here`
let regex = /bcolour:s+(d+)/;
let found = body.match(regex);
//console.log(found[1]); // Uncaught TypeError: Cannot read property '1' of null
// This snippet below is safe to use :
if (found) {
console.log(found[1]);
} else {
console.log('not found');
}
body = `size: 8 colour: 1`
let regex = /bcolour:s+(d+)/;
let found = body.match(regex);
console.log(found[1]);
body = `size: 8 colour: 1`
let regex = /bcolour:s+(d+)/;
let found = body.match(regex);
console.log(found[1]);
body = `size: 8 but unfortunately, no colour here`
let regex = /bcolour:s+(d+)/;
let found = body.match(regex);
//console.log(found[1]); // Uncaught TypeError: Cannot read property '1' of null
// This snippet below is safe to use :
if (found) {
console.log(found[1]);
} else {
console.log('not found');
}
body = `size: 8 but unfortunately, no colour here`
let regex = /bcolour:s+(d+)/;
let found = body.match(regex);
//console.log(found[1]); // Uncaught TypeError: Cannot read property '1' of null
// This snippet below is safe to use :
if (found) {
console.log(found[1]);
} else {
console.log('not found');
}
edited 12 hours ago
answered 16 hours ago
Pac0
7,16722443
7,16722443
add a comment |
add a comment |
Tamilmani Natarajan is a new contributor. Be nice, and check out our Code of Conduct.
Tamilmani Natarajan is a new contributor. Be nice, and check out our Code of Conduct.
Tamilmani Natarajan is a new contributor. Be nice, and check out our Code of Conduct.
Tamilmani Natarajan is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53183964%2fhow-to-parse-the-key-and-get-its-value-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password