Regular expression, get data from base64 [duplicate] - javascript

This question already has answers here:
How to strip type from Javascript FileReader base64 string?
(5 answers)
What do 'lazy' and 'greedy' mean in the context of regular expressions?
(13 answers)
Closed 3 years ago.
I have a base64 data
data:application/pdf;base64,JVBERi0xLj........
I want to get the rest of the data like
JVBERi0xLj........
Following not working, wonder what is the correct approach?
const express = /data:(.*?);base64,(.*?)/g;
const arr = express.exec(base64);

You can simplify and match all the data uri standard :
:([^;]*)[^,]+,(.*)
Your Regex will not match other data urls that are also standard like :
data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh.
data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678

Related

Encode array contents with base64 [duplicate]

This question already has answers here:
How can you encode a string to Base64 in JavaScript?
(33 answers)
Closed 2 years ago.
I want to encode my array contents with base64 if possible in javascript (and then decode later).
Example:
var array = ["stack", "overflow"]
// base64.encode(array)
Code:
var array = ["stack", "overflow"]
array.map(btoa);
In order to use the well-known function btoa, you'll first have to convert your array to string, in such a way that you can reverse the operation. JSON would be the string format to go for.
So to encode do:
base64 = btoa(JSON.stringify(array))
To decode do:
JSON.parse(atob(base64))

string escaping in json object [duplicate]

This question already has answers here:
How should I escape strings in JSON?
(18 answers)
Closed 3 years ago.
I have this object that I send in response as json.
{"__type":"http:\\/\\/example.com\\/contracts\\/documents\\/rendering\\/instructions\\/1\\/0"}
I want response to be:
{"__type":"http:\/\/example.com\/contracts\/documents\/rendering\/instructions\/1\/0"}
but I get this:
{"__type":"http:\\/\\/example.com\\/contracts\\/documents\\/rendering\\/instructions\\/1\\/0"}
How do I escape string correctly, so I can get response string with only one backslash?
Do this:
let str='{"__type":"http:\\/\\/example.com\\/contracts\\/documents\\/rendering\\/instructions\\/1\\/0"}'
let result = str.replace(/\\/g,'\\');

removing file extension in strings in javascript [duplicate]

This question already has answers here:
How can I get file extensions with JavaScript?
(36 answers)
Regular expression to remove a file's extension
(9 answers)
How to trim a file extension from a String in JavaScript?
(27 answers)
Closed 4 years ago.
I have to parse strings that present name of files as:
myfilename.txt
anotherfilename.jpg
a.filename.with.dots.jpg
I need to extract the filename without it's extension.
for example "myfilename.txt" should return "myfilename"
and "a.filename.with.dots.jpg" should return "a.filename.with.dots"
not that some filename are problematic in what they contain and the javascript should be able to handle this.
what's the best was of doing this?
this is what I tried so far and didn't work.
namearr = name.split('.')
filename = namearr.splice()
I can use regex like
^(.?)..?$
for this but it seems like an overkill for this
the problem I see with other answered for this is that they don't take in account the number of dots the filename can contain. (not excluding the extension)

getting a number using regular expressions [duplicate]

This question already has answers here:
Regular expression for extracting a number
(4 answers)
Closed 6 years ago.
I have the following string:
PR-1333|testtt
I want to get the number 1333 using regular expressions in javascript. How can I do that?
This will look for numbers in your text.
var text = "PR-1333|testtt";
var number = text.match(/\d+/g); // this returns an array of all that it found.
console.log(number[0]); // 1333

Specific Regular Expression In Javascript with a condition [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Parsing an URL in JavaScript
(3 answers)
Closed 8 years ago.
How to format a regular expression In JavaScript :
http://localhost:3000/sales?&tp=Home+Stay&am=Pool&pl=1620&pt=Flash+Sale&st=5
I want to get &pl= and all the digits after &pl=.
So the result would be &pl=1620 in this case.
Please help how to perform this??
Your help means a lot for me.
This script will do the trick:
var regex = /&pl=\d*/;
var match = regex.exec(yourtext)[0];
Following regex should do it
/&pl=\d*/.exec('http://localhost:3000/sales?&tp=Home+Stay&am=Pool&pl=1620&pt=Flash+Sale&st=5')[0]
Demo: Fiddel

Categories