I need to have an array as following but the single quote before (s) causes error. JavaScript does not accept them. Is there any way to bypass the error or replace the single quotes with other characters like space?
I tried to use replace function but not sure how to use it. I used ' but it did not work.
var locations = [
[
'Alex's loc', '37.9908372',
'23.7383394', '0'
],
[
'John James's loc', '37.9908372',
'23.7383394', '1'
],
[
'Norman's loc', '38.075352',
'23.807885', '3'
],
[
'Jack Moore's loc', '37.9908372',
'23.7383394', '2'
]
];
Code
var locations = [
<c:forEach var="location" items="${locationes}" varStatus="loop">[
'${location.value.name}', '${location.value.latitude}',
'${location.value.longitude}', '${loop.index}', </c:forEach> ];
you can wrap the single quote with double quote, like:
var locations = [
[
"Alex's loc", '37.9908372',
]
];
Using a backslash \ character before the invalid quote will also work. This is called an escape sequence
'Alex\'s loc' // this is represented as the string => Alex's loc
This is also how you get litteral new lines in strings
'\n' or "\n" for new line.
Related
I have a string:
for (;;); {
"__ar": 1,
"payload": null,
"jsmods": {
"require": [
["ServerRedirect", "redirectPageTo", [],
["https:\/\/bigzipfiles.facebook.com\/p\/dl\/download\/file.php?r=100028316830939&t=100028316830939&j=11&i=5823694&ext=12121516&hash=AaBVNURld6wrKBcU", true, false]
]
],
"define": [
["KSConfig", []
}
I try to regex this to be:
https://bigzipfiles.facebook.com/p/dl/download/file.php?r=100028316830939&t=100028316830939&j=11&i=5823694&ext=12121516&hash=AaBVNURld6wrKBcU
I've used
var results = $(document).find("pre").html();
var regex1 = new RegExp(/["\w\.\/\;\?\=\-\&\\\\"]/);
var resultsReplace = regex1.exec(results);
console.log(resultsReplace);
but it is not working.
Can anyone help me, please?
Assuming it is always URL and the delimiter is always " we can use a much simpler regex here, like:
str.match(/http[^"]+/)[0]
So we looking for a string starting with http with any following characters except " since the match string never can get one because this is a delimiter.
LMK if cases are wider (not an url, may not start with http, etc.) and I'll adjust regex.
When I view-source a html page, I saw this in text/javascript tag:
playlist = [{
title: "",
thumnail: "//example.com/folder/c9cc7f89fe5c168551bca2111d479a3e_1515576875.jpg",
source: "https://examp.com/360/HX62.mp4?authen=exp=1517246689~acl=/82vL3DDTye4/*~hmac=977cefd9de63a29fde25c856e0fdfd2f",
sourceLevel: [
{
source: "https://examp.com/360/HX62.mp4?authen=exp=1517246689~acl=/82vL3DDTye4/*~hmac=977cefd9de63a29fde25c856e0fdfd2f",
label: '360p'
},
{
source: "https://examp.com/480/HX62.mp4?authen=exp=1517246689~acl=/SuCa7NnGEhM/*~hmac=80bc89a07b1f4ed87d584a89c623e946",
label: '480p'
},
{
source: "https://examp.com/720/HX62.mp4?authen=exp=1517246689~acl=/SuCa7NnGEhM/*~hmac=80bc89a07b1f4ed87d584a89c623e946",
label: '720p'
},
],
}];
I want to get strings in source and label, then I've write this code:
$page = curl ('https://example.com/video-details.html')
preg_match ('#sourceLevel:[{source: "(.*?)",label: \'360p\'},{source: "(.*?)",label: \'480p\'},{source: "(.*?)",label: \'720\'}#', $page, $source);
$data360 = $source[1];
$data480 = $source[2];
$data720 = $source[3];
echo $data360. '<br/>' .$data480. '<br/>' .$data720. '<br/>';
I know it can be wrong in somewhere, because I'm new to PHP. I'm hoping there is someone help me to correct my code. Many thanks!
You need to:
escape braces and square brackets in your regular expression as they have special meanings in regexes,
escape the single quotes in the string literal for which you chose the single quote as delimiter (which you corrected after I wrote this).
provide for the white space that can appear between several characters (e.g. before and after {) in your page string.
I would also suggest to match the source/labels each as separate matches, so that when there are not exactly three, you will still have them all.
Here is the suggested code:
preg_match_all('~\{\s*source\s*:\s*"(.*?)"\s*,\s*label\s*:\s*\'(.*?)\'\s*\}~',
$page, $sources);
$sources = array_combine($sources[2], $sources[1]);
This will provide the $sources variable as an associative array, keyed by the labels:
[
"360p" => "https://examp.com/360/HX62.mp4?authen=exp=1517246689~acl=/82vL3DDTye4/*~hmac=977cefd9de63a29fde25c856e0fdfd2f",
"480p" => "https://examp.com/480/HX62.mp4?authen=exp=1517246689~acl=/SuCa7NnGEhM/*~hmac=80bc89a07b1f4ed87d584a89c623e946",
"720p" => "https://examp.com/720/HX62.mp4?authen=exp=1517246689~acl=/SuCa7NnGEhM/*~hmac=80bc89a07b1f4ed87d584a89c623e946"
]
I got an array containing strings.
I want to make sure there are no space fillings in end or start of string, in middle is ok.
So I've tried using lodash by doing this:
var answers = req.body.answer.split(/;,/); // req.body.answer = 'nio,9'
answers = _.map(answers, _.trimEnd);
answers = _.map(answers, _.trimStart);
The result is this:
[ 'nio , 9' ] // answer before trim
[ 'nio , 9' ] // answer after trim
The wanted result is:
[ 'nio', '9']
I think the problem in your code is your regular expression you are using, you are trying to split the string where there are ;, and I'm guessing you just want to split by , since your result is an array with only one string inside:
[ 'nio , 9' ]
you should use this regular exp instead:
var answers = req.body.answer.split(/,/);
or without any regular exp just do:
var answers = req.body.answer.split(',');
I'm currently working within an AngularJS directive and in the template I'm attempting to check if a an instance variable of the Controller is a certain type of string.
Specifically, this string can be anything at all so long as it has an 8-digit number in it.
Passing Examples: "gdbfgihfb 88827367 dfgfdg", "12345678", ".12345678"
The number has to be a solid string of 8 numbers with nothing in between.
I've tried this:
$ctrl.var == /[0-9]{8}/
But it doesn't work for some reason. How do I construct a regex in order to do this?
Thanks
Your regex is fine but the comparison is wrong. You want
/\d{8}/.test($ctrl.var)
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
let tests = ["gdbfgihfb 88827367 dfgfdg", "12345678", ".12345678", "nope, no numbers here"],
rx = /\d{8}/;
tests.map(str => document.write(`<pre>"${str}": ${rx.test(str)}</pre>`))
Code:
var first = "gdbfgihfb 88827367 dfgfdg";
var second = "12345678";
var third = ".12345678";
var reg = new RegExp('[0-9]{8}');
console.log(first.match(reg));
console.log(second.match(reg));
console.log(third.match(reg));
Output:
[ '88827367', index: 10, input: 'gdbfgihfb 88827367 dfgfdg' ]
[ '12345678', index: 0, input: '12345678' ]
[ '12345678', index: 1, input: '.12345678' ]
I have a JSON as below:
[{"type":"Point","coordinates":"[-77.03,38.90]"},"properties":{"city":3,"url":"xyz.com"}]
I want to replace "[ with [ and ]" with ]
try this
$yourJson = [{"type":"Point","coordinates":"[-77.03,38.90]"},"properties":{"city":3,"url":"xyz.com"}];
$jsonString=preg_replace('/"([^"]+)"\s*:\s*/', '$1:', $yourJson);
$stringReplace=str_replace('"[', '[', $jsonString);
$stringReplace=str_replace(']"', ']', $stringReplace);
echo $stringReplace;
from the comment it seems you are putting additional quote " in replace try this
json = json.replace("\"[","[");
You can use the following regex:
str.replace(/"(\[[^"\]]*\])"/, "$1");
where str is the input string. This will match "[ followed by a string of any characters that are not " and ], followed by ]" and drop the two " at the extremes. In your case it will turn "[-77.03,38.90]" into [-77.03,38.90].
Aside from that, whoever coded what is returning the string in the question should be fired immediately.