I want to fetch data from a webpage and search for a particular text in it. I am able to do it using python but i want to do it using javascript. I am attaching the python code so that you can understand what i am trying to do, and how I can do it using javascript.
import requests
a = requests.get("http://localhost:3000/data")
b = a.text
if("wrong" in b):
print(b)
Any help will be appreciated.
CodePudding user response:
You can do a regex search in javascript like so
fetch('http://google.com')
.then(function (response) {
switch (response.status) {
// status "OK"
case 200:
let text = response.text();
let n = text.search("W3Schools");
console.log(text,n)
// status "Not Found"
case 404:
throw response;
}
})
.then(function (template) {
console.log(template);
})
.catch(function (response) {
// "Not Found"
console.log(response.statusText);
});