Exclude characters from string before passing it to encodeURIComponent() - javascript

If a string contains character from interval U+D800..U+DFFF then encodeURIComponent() throws a malformed URI sequence error. I would like to eliminate those characters from a given string before passing it to encodeURIComponent(). How to do that?
Example:
I have a textfile encoded in UTF-16BE which contains the following hexa chars:
D7FF D800 D801 ... DFFE DFFF E000
I'm searching for a function which returns this string from the string above:
D7FF E000
So only valid Unicode characters remain.

You can use a replace/encodeURIComponent combo to achieve the desired result. You first need to match all the characters that do not fall in the unicode range [0xD800..0xDFFF] using this regex: /[^\uD800-\uDFFF]+/g then replace them with their encoded versions:
let result = string.replace(/[^\uD800-\uDFFF]+/g, match => encodeURIComponent(match));
Example:
let string = "/foo/\uD7FF\uD800\uD801/bar";
let result = string.replace(/[^\uD800-\uDFFF]+/g, match => encodeURIComponent(match));
console.log(result);

Related

How to remove specific multiple string and then remain another?

let result = 'Apple%00Juice%02';
const removeOne = result.slice(5, 8); // get %00
const removeTwo = result.slice(13, 16); // get %02
slice get the part of I want to remove not I want to get.
Is any function can let me get the result becomes to 'Apple Juice' ?
You can get the desired result using a regex to match the parts you want to remove from the string and then replace them using the replace() method on strings.
const str = "Apple%00Juice%02";
const regex = /%\d+/g;
const result = str.replace(regex, " ").trim()
console.log(result);
Explanation of regex:
% - match the character % literally
\d+ - match any digit 0 to 9 one or more times
%\d+ - match % character followed by one or more digits
You can achieve this by using .replace() function
Example:
let result = 'Apple%00Juice%02';
result = result.replace('%00', ' ');
result = result.replace('%02', '');
console.log(result);
Read More About .replace() function at MDN Docs
Edit:
Minifying #Yousaf's Answer
let result = "Apple%00Juice%02";
result = result.replace(/%\d+/g, " ").trim()
console.log(result);
It is possible with replace(), but that is not a sustainable solution. The URL encoding "% 00" is the � ASCII character. This suggests that the string is already being encoded in the wrong character format for the URL. So you have to look at the character format in which your database or file is read out. for example UTF-8, ISO 8859-1
When encoded in the correct character format. Can you decode it in JavaScript using the decodeURIComponent (str) method.
more on this

Regular Express for Javascript - Contain a specific word in the beginning after get any character until a certain character comes

I need a certain type of regular expression where I need list of special type of strings from a string. Example input:
str = 'this is extra data which i do not need /type/123456/weqweqweqweqw/ these are more extra data which i dont need /'
Result needed:
/type/123456/weqweqweqweqw/
Here the /type/ string will be constant and the remaining will be dynamic i.e. 123456/weqweqweqweqw and the last string will be /.
I tried:
var myRe = /\/type\/(.*)\//g
But this matches everything from /type/ to the end of the string.
Instead of repeating ., which will match anything, repeat anything but a space via \S+, so that only the URL part of the string will be matched:
const str = 'this is extra data which i do not need /type/123456/weqweqweqweqw/ these are more extra data which i dont need /';
console.log(str.match(/\/type\S+/));
It's tagged Python, so here is a solution:
import re
re.search(r"/type/[^/]*/[^/]*/",str)
Out: <_sre.SRE_Match object; span=(39, 66), match='/type/123456/weqweqweqweqw/'>

Regex to match number after string without the matching string

I'm trying to write a regex in javascript to match a series of numbers after a particular string without getting the string in result. So far, I have come up with this:
(?!smart_id=)[0-9]+
which is to be tested against strings like:
ksld8403smart_id=9034&kqwop
discid=783&smartid=83234&ansqw
fdsjfnfd3209sdf&smart_id=2102&hjg
but I'm getting both the numbers before and after smart_id. The tests need to be performed on https://regexr.com/
You can use regex along with array#map. Once you have matched result, you can array#split the result on = and get the value at the first index.
var str = `ksld8403smart_id=9034&kqwop
discid=783&smartid=83234&ansqw
fdsjfnfd3209sdf&smart_id=2102&hjg`;
console.log(str.match(/(smart_id=)(\d+)/g).map(matched => +matched.split('=')[1]));

Javascript to cut same second character on a text

For example I have text:
var x="default_1305, default_1695, default_1805";
I want to cut before the second comma to get this text:"default_1305, default_1695".
How can I do this?
var x="default_1305, default_1695, default_1805";
string can be split by , like below:
var res = x.split(",", 2);
Note 2 here in the second param.
And if needed as string, then
var res_string = res.join(",");
Edit:
.split() on MDN
Syntax
str.split([separator[, limit]])
Parameters
separator
Optional. Specifies the character(s) to use for separating the string. The separator is treated as a string or a regular expression. If separator is omitted, the array returned contains one element consisting of the entire string. If separator is an empty string, str is converted to an array of characters.
limit
Optional. Integer specifying a limit on the number of splits to be found. The split() method still splits on every match of separator, until the number of split items match the limit or the string falls short of separator.
Convert string to array and get first two elements
var x="default_1305, default_1695, default_1805";
var b = x.split(',')
var c = b[0]+","+b[1]
You can also use .slice() to get the parts you need, eg:
// added an extra item to distinguish first-two vs all-but-last
var x="default_1305, default_1695, default_1805, default_1962";
// get first two
var result = x.split(",").slice(0,2).join(",");
console.log(result);
// get all but last
var result = x.split(",").slice(0,-1).join(",");
console.log(result);

Javascript replace url with string

I have a string of url encoded characters.
wondering if there is a javascript function that can replace all the url encoded characters with normal string characters.
i know i can use the replace function, but it only replaces one certain character an not all at once
for example, I am looking for one function that will replace all the url encoded characters in this string:
string urlEncoded = '1%20day%20%40work!%20Where%20are%20you%3F'
and give me this string:
string replaced = '1 day # work! Where are you?'
thanks a lot
Use decodeURIComponent(string) for that purpose.
string urlEncoded = '1%20day%20%40work!%20Where%20are%20you%3F';
string replaced = decodeURIComponent(urlEncoded);
alert(replaced);
More info here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/decodeURIComponent
string replaced = decodeURIComponent(urlEncoded);
There is also just decodeURI but this does not cope with "special" characters, such as & ? ! # etc
Use decodeURIComponent(urlEncoded)
You are looking for unescape
var decoded = unescape(urlEncoded);

Categories

Resources