How to avoid inserting undefined in MySQL using nodejs and reactjs? - javascript

Is there a way to stop inserting an undefined value in MYSQL using reactjs?
so here is the code.
const regex = /.*([A-Za-z0-9_\-]{11}).*/gi;
if (postData.video_id) {
postData.video_id = postData.video_id.replace(regex, "$1");
}
and it keeps submitting undefined in the database, I need to make it optional either with value or empty, it is up to the user.
appreciate your help

the data might do not match with the regex, so
validate the replace expression before assign it to postData.video_id :
const regex = /.*([A-Za-z0-9_\-]{11}).*/gi;
if (postData.video_id) {
const videoId = postData.video_id.replace(regex, "$1");
if(videoId) postData.video_id = videoId;
}

Related

How do I replace all the / in a URL with ,?

I'm trying to convert a URL input to a suitable format for my api and have run into an issue.
Whenever I perform a global search and replace using (/blue/g, "red"); it works fine.
But whenever I try to do the same operation, but with slashes ((///g, "red");) it gives me a
syntax error.
Is there a way to get around this?
var strings = "yellow/red/blue";
console.log(strings);
strings = strings.replace(/\//g,",");
console.log(strings);
This should work.
You are searching for replace / by , in URL:
Here is the solution for it:
const windowURL= 'https://stackoverflow.com/questions/65318599/how-do-i-replace-all-the-in-a-url-with';
const URL = new URL(windowURL);
const path = URL.pathname.replaceAll('/', ',')
console.log('Updated path in which `/` is replaced by `,` =>>', path)

Javascript regex parse complex url string

I need to parse a complex URL string to fetch specific values.
From the following URL string:
/api/rss/feeds?url=http://any-feed-url-a.com?filter=hot&format=rss&url=http://any-feed-url-b.com?filter=rising&format=rss
I need to extract this result in array format:
['http://any-feed-url-a.com?filter=hot&format=rss', 'http://any-feed-url-b.com?filter=rising&format=rss']
I tried already with this one /url=([^&]+)/ but I can't capture all correctly all the query parameters. And I would like to omit the url=.
RegExr link
Thanks in advance.
This regex works for me: url=([a-z:/.?=-]+&[a-z=]+)
also, you can test this: /http(s)?://([a-z-.?=&])+&/g
const string = '/api/rss/feeds?url=http://any-feed-url.com?filter=hot&format=rss&url=http://any-feed-url.com?filter=latest&format=rss'
const string2 = '/api/rss/feeds?url=http://any-feed-url.com?filter=hot&format=rss&next=parm&url=http://any-feed-url.com?filter=latest&format=rss'
const regex = /url=([a-z:/.?=-]+&[a-z=]+)/g;
const regex2 = /http(s)?:\/\/([a-z-.?=&])+&/g;
console.log(string.match(regex))
console.log(string2.match(regex2))
have you tried to use split method ? instead of using regex.
const urlsArr = "/api/rss/feeds?url=http://any-feed-url-a.com?filter=hot&format=rss&url=http://any-feed-url-b.com?filter=rising&format=rss".split("url=");
urlsArr.shift(); // removing first item from array -> "/api/rss/feeds?"
console.log(urlsArr)
)
which is going to return ["/api/rss/feeds?", "http://any-feed-url-a.com?filter=hot&format=rss&", "http://any-feed-url-b.com?filter=rising&format=rss"] then i am dropping first item in array
if possible its better to use something else then regex CoddingHorror: regular-expressions-now-you-have-two-problems
You can matchAll the url's, then map the capture group 1 to an array.
str = '/api/rss/feeds?url=http://any-feed-url-a.com?filter=hot&format=rss&url=http://any-feed-url-b.com?filter=rising&format=rss'
arr = [...str.matchAll(/url=(.*?)(?=&url=|$)/g)].map(x => x[1])
console.log(arr)
But matchAll isn't supported by older browsers.
But looping an exec to fill an array works also.
str = '/api/rss/feeds?url=http://any-feed-url-a.com?filter=hot&format=rss&url=http://any-feed-url-b.com?filter=rising&format=rss'
re = /url=(.*?)(?=&url=|$)/g;
arr = [];
while (m = re.exec(str)) {
arr.push(m[1]);
}
console.log(arr)
If your input is better-formed in reality than shown in the question and you’re targeting a modern JavaScript environment, there’s URL/URLSearchParams:
const input = '/api/rss/feeds?url=http://any-feed-url-a.com?filter=hot%26format=rss&url=http://any-feed-url-b.com?filter=rising%26format=rss';
const url = new URL(input, 'http://example.com/');
console.log(url.searchParams.getAll('url'));
Notice how & has to be escaped as %26 for it to make sense.
Without this input in a standard form, it’s not clear which rules of URLs are still on the table.

Does string contain Ajax?

I am writing a tutorial page for JavaScript and to prevent xss I want to check if the user's input contains any ajax and if it does return an error string. Also any other elements that could be used for xss in the input should make it error. What would the code for this be?
Try defining a RegExp including methods, strings which should not pass validation , utilize RegExp.prototype.test() with string as argument
var re = /XMLHttpRequest|.\.ajax|.\.get|.\.post|.\.getScript|script/;
var str = "$.post";
var res = re.test(str) ? new Error("error") : str;
console.log(res)

TyperError: urlpage.match(...) is null JavaScript regex

I want to take a value from an URL an put it on an input.
So,
Normal URL: www.mysite.com
URL with value: www.mysite.com/mypage.php?variabletext
So, I want to extract ?variabletext (and, yes, it´s always a different text string)
I´m using this function
$(document).ready(function(){
var urlpage = window.location.href;
regex = /http\:\/\/www\.mysite\.com\/mypage.php\?([a-zA-Z]+)/;
var texturl = urlpage.match(regex)[1];
if(texturl !== null)
{
document.getElementById('idmyinput').value= texturl;
}
});
Now the problem.
If i put a value, for example, www.mysite.com/mypage.php?bob
It works well.
But if it´s not a value, example, www.mysite.com/mypage.php
I receive this error:
TyperError: urlpage.match(...) is null
My idea is that only change the value of the input if the variable texturl has a value, aka, is not null.
Anyone can help me...?
Thanks a lot
If urlpage.match(regex) is null (that is, there is no match), then urlpage.match(regex)[1] will throw.
So you can use
var match = urlpage.match(regex);
if(match) {
var texturl = match[1];
// Use texturl
}
However, parsing URLs manually is a bad idea, use a URLUtils property instead. In this case, search:
The URLUtils.search property is a DOMString containing a ?
followed by the parameters of the URL.
So you can use
var texturl = location.sarch.substr(1);
str =str.replace(/[^a-zA-Z0-9]/g,"");
str = str.replace(/httpwwwmysitecommypagephp/g,"");

How to remove " from my Json in javascript?

I am trying to inject json into my backbone.js app. My json has " for every quote.
Is there a way for me to remove this?
I've provided a sample below:
[{"Id":1,"Name":"Name}]
Presumably you have it in a variable and are using JSON.parse(data);. In which case, use:
JSON.parse(data.replace(/"/g,'"'));
You might want to fix your JSON-writing script though, because " is not valid in a JSON object.
Accepted answer is right, however I had a trouble with that.
When I add in my code, checking on debugger, I saw that it changes from
result.replace(/"/g,'"')
to
result.replace(/"/g,'"')
Instead of this I use that:
result.replace(/(&quot\;)/g,"\"")
By this notation it works.
var data = $('<div>').html('[{"Id":1,"Name":"Name}]')[0].textContent;
that should parse all the encoded values you need.
This is a simple way to replace &quot with what you need to change it - chars, strings etc.
function solve(input) {
const replaceWith = '"' // e.g. replace " by "
const result = input.replace(/"/g, replaceWith)
return result;
}
console.log(solve('{"x":"1","y":"2","z":"10"}')
The following works for me:
function decodeHtml(html) {
let areaElement = document.createElement("textarea");
areaElement.innerHTML = html;
return areaElement.value;
}
In my case "quot" was replaced with other characters so I found a stupid but working workaround
str.replace(/&qu/g,'').replace(/ot\;/g,'')
i used replace feature in Notepad++ and replaced " (without quotes) with " and result was valid json
IF you are using SQL then you can use:
update tablename
set payload = replace(payload, '&', chr(39))
where id = 'unique id';
If you are using python then you can use:
import html
l = [{"Id":1,"Name":"Name}]
r = html.unescape(l)

Categories