Formatting bible verse reference with regex in javascript - javascript

I'm trying to monospace bibleverse references so that single digit chapters or verses have a leading space.
So "4:5" becomes " 4: 5" and "3:21" becomes " 3:21".
I'm really having problems writing the regex, please help.
I've tried many variations but they essentially boil down to (^\d|\d$), (^\d{1}|\d{1}$) and (^[^0-9]\d|[^0-9]\d$) and many combinations between them
inRef = inChapter + ':' + inVerse;
var inReg = /(^[0-9]{1}|[^0-9][0-9]{1}$)/g;
inRef = inRef.replace(inReg," $1");
console.log(inRef);
Alot of the results I'm getting from my efforts turn references like "6:15" into " 6: 1 5" or " 6:1 5"
Thank you in advance.

Why a regex at all? You've already got the chapter/verse as separate data BEFORE you combined them into the x:y format, so do the formatting there while they're still seperate strings:
if (inChapter.length == 1) { inChapter = ' ' + inChapter }
inRef = inChapter + ':' + inVerse;
Using a regex for this uber-simplistic transformation is akin to nuking a city to get some dust off a shelf instead of using a feather duster.

Given the strings inChapter and inVerse, you could do something like this:
inRef = (" " + inChapter).slice(-2) + ":" + (" " + inVerse).slice(-2)
Note there are two spaces there " " and I'm assuming inChapter and inVerse are only ever 1 or 2 digits.
Edit: Since you need three digits and I assume you still want these to line up, you could do this:
var pad = " "; // this is now THREE spaces!
inRef = (pad + inChapter).slice(-pad.length) + ":" + (pad + inVerse).slice(-pad.length)
So now if you run all your inChapter and inVerse pairs through this, you should get strings that line up like this:
100:100
20:100
2:100
100: 10
100: 1
10: 10
10: 1
1: 1

Related

why doesnt the persian word add behind a number?

i have a really strange problem in java script.
look at these codes and run the app:
const number = 200000;
const persianMoney = "تومان";
//pay attention to these lines:
console.log("way 1:");
console.log(number + persianMoney);
console.log(persianMoney + number);
console.log("way 2:");
console.log(`${number}${persianMoney}`);
console.log(`${persianMoney}${number}`);
console.log("way 3:");
console.log(String(number) + persianMoney);
console.log(persianMoney + String(number));
console.log("way 4:");
console.log(`${String(number)}${persianMoney}`);
console.log(`${persianMoney}${String(number)}`);
console.log("way 5:");
console.log(String(number + persianMoney));
console.log(String(persianMoney + number));
all of the outputs are the same!! all of the outputs are 200000تومان! but why word تومان is not behind 200000 in some outputs? even here, i cant put تومان behind 200000! but why it is like that? i cant understand it. i tested concatenation in 5 ways but none of them were correct! how can i solve this problem? thanks for helping.
As stated by Raymand Chen. My understanding is that you want to have the currency name تومان after the number 200000. To keep the Arabic/Persian word in the same direction as the Latin word i.e. in a Left-To-Right LTR direction.
One possible way to do that is to add the LTR code \u200E
const number = 200000;
const persianMoney = "تومان";
const ltr = "\u200E";
console.log(persianMoney + ltr + " " + number);

print in new line in callback function in javascript

This is my code for callback function in javascript:
callback(
undefined,
body.current.weather_descriptions[0] +
"." +
"It is currently " +
body.current.temperature +
" degrees. It feels like " +
body.current.feelslike +
" degrees. There is " +
body.current.precip +
"% chance of rain."
);
My desired output is:
Varanasi, Uttar Pradesh, India
Partly cloudy.
It is currently 26 degrees.
It feels like 26 degrees.
There is 0% chance of rain.
But my output coming is:
Varanasi, Uttar Pradesh, India
Partly cloudy.It is currently 26 degrees. It feels like 26 degrees. There is 0% chance of rain.
I tried many things but not getting desired output, can you help ?
Ok to literally throw back at you the solved version of what you gave us..
callback(
undefined,
body.current.weather_descriptions[0] +
".\n" +
"It is currently " +
body.current.temperature +
" degrees.\nIt feels like " +
body.current.feelslike +
" degrees.\nThere is " +
body.current.precip +
"% chance of rain."
);
On mobile, so excuse the lack of formatting.
Add in “\n\r” each place you want to add a new line. So something like
“It is currently: “ + your.call.data + “\n\rdegrees outside”.
If you need to display this in a browser, use "<br>" tag to places where you need the new lines:
callback(
undefined,
body.current.weather_descriptions[0] +"<br>"
"." +
"It is currently " +
body.current.temperature +
" degrees." +"<br>" "It feels like " +
body.current.feelslike
" degrees." +"<br>"+ "There is " +
body.current.precip +
"% chance of rain."
);
If you need to display this in a console, use "\n" to places where you need the new lines:
callback(
undefined,
body.current.weather_descriptions[0] +"\n"
"." +
"It is currently " +
body.current.temperature +
" degrees." +"\n" "It feels like " +
body.current.feelslike
" degrees." +"\n"+ "There is " +
body.current.precip +
"% chance of rain."
);
You can insert literal newline character in a string with \n. For example:
const myString = "Hello,\nThat's a nice Tnettenba.";
console.log(myString);
Hello,
That's a nice Tnettenba.
Or, you can use backticks to create a string literal with newlines. For example:
const anotherString = `Hello,
That's a nice Tnettenba.`;
Will produce the same output as above.
Using strings delineated with backticks is helpful in this situation, as any whitespace (newlines, indents) are preserved, which is often desirable when formatting text for display. Eg.
const myTable = `
| ID | Name |
-------------------------
| 001 | Hello, World! |
| 002 | Brave New World |
`;
Will output:
| ID | Name |
-------------------------
| 001 | Hello, World! |
| 002 | Brave New World |
Note the leading spaces.

Why is the following way to assign a value to a JSON array not working?

I have this code:
compareList[productName] = productID + ',' + productHref;
console.log(productName + ' ' + productID + ' ' + productHref + ' ' + compareList.length);
Which logs into this (I have removed the link):
Acer Iconia B1-790 [NT.LDFEE.002] 112576 link removed for confidentiality 0
As you can see, all three variables are valid strings, but the json object still fails to assign (compareList.length logs as 0). I've been thinking and thinking but I simply can't figure it out. Any help is appreciated.
Maybe this version of adding and checking array length can be useful to you?
var compareList=[]
var productName = {productID:'saban',productHref:'http://saulic.com'};
compareList.push(productName);
console.log(compareList.length);

Regex to validate custom tracking information

I am working on a requirement to validate tracking information with the following restrictions:
Format: COPYYYY#####
COP is a fixed prefix of each tracking information
YYYY is a year in which the tracking information was submitted (valid years: 2015-2018)
##### is a randomly generated 5 digit number
Tracking information should only have 3 letter characters (COP prefix)
Tracking information should have exactly 9 numeric characters that follow the first 3 letter characters.
Tracking information should be exactly 12 characters long
Since regex is not great solution to validate number range, I decided to check year later once the format is valid.
COP followed by 9 digits (i.e COPXXXXXXXXX)
The regex below is always returning false, even for correct inputs.
/^COP\d{9}$/
Please suggest corrections to above regex and also share thoughts on validating year range in regex (if it is cleaner approach).
Use regex pattern \bCOP(201[5-8])(\d{5})\b
Test source code (JavaScript):
var re = /\bCOP(201[5-8])(\d{5})\b/g;
var s = 'To track your packages, enter COP201812345 and COP201867890 at www.example.org';
var m;
do {
m = re.exec(s);
if (m) {
var e = document.createElement('div');
e.innerHTML = 'Match: ' + m[0] + ' (Year: ' + m[1] + ', Id: ' + m[2] + ')';
document.getElementById("output").appendChild(e);
}
} while (m);
Test source code (Java):
String s = "To track your packages, enter COP201812345 and COP201867890 at www.example.org";
Matcher m = Pattern.compile("\\bCOP(201[5-8])(\\d{5})\\b").matcher(s);
while (m.find())
System.out.println(
"Match: " + m.group() +
" (Year: " + m.group(1) + ", Id: " + m.group(2) + ")");
}
Output:
Match: COP201812345 (Year: 2018, Id: 12345)
Match: COP201867890 (Year: 2018, Id: 67890)
Test it here (JavaScript) and here (Java).
Use the code below.
String value = "COP201812345";
Pattern p = Pattern.compile("^(COP)(201[5-8])(\\d{5})$");
Matcher m = p.matcher(value);
if (m.find()) {
System.out.println("Tracking number " + value + " is valid");
System.out.println("Tracking prefix: " + m.group(1));
System.out.println("Year between 2015 and 2018 is: " + m.group(2));
System.out.println("Random 5 digit number is: " + m.group(3));
}
else {
System.out.println("No match");
}
Output:
Tracking number COP201812345 is valid
Tracking prefix: COP
Year between 2015 and 2018 is: 2018
Random 5 digit number is: 12345

Why does "Brain" print 4 times and not 5 in this code snippet?

I'm doing some JavaScript koans to learn syntax and I came across one case where I'm a bit confused. This is the code:
it("should know properties that are functions act like methods", function () {
var megalomaniac = {
mastermind : "Brain",
henchman: "Pinky",
battleCry: function (noOfBrains) {
return "They are " + this.henchman + " and the" +
Array(noOfBrains + 1).join(" " + this.mastermind);
}
};
var battleCry = megalomaniac.battleCry(4);
expect("They are Pinky and the Brain Brain Brain Brain").toMatch(battleCry);
});
Because the battleCry function creates an array with noOfBrains + 1 elements, and noOfBrains is passed in as 4 I would have expected the join() to print 5 "Brain" not 4. Why is it 4?
Thanks!
You get an array of size 5 (= 4 + 1). You then join these together.
Note that the Brain (the glue) is only applied to the joined parts. As your array is size 5, you need 4 "glue parts" to create a String.
Hence the 4 Brain.
If one did something as
Array(noOfBrains + 1).map(e => 'Brain').join(' ')
you would indeed get 5 items

Categories