1592956380
We recently did some improvements on the elmah.io UI making IPs clickable. We’ve had automatic detection of IPs for a while but it always bugged me that the regex that we had didn’t caught IP addresses with a port number (typically logged by ASP.NET MVC and similar):
127.0.0.1:80
The solution was an improved piece of JavaScript to detect this that I’m sharing in hope of helping others with a similar challenge:
function resolveIP(data) {
if (data) {
if(data.match(/^(?:(?:2[0-4]\d|25[0-5]|1\d{2}|[1-9]?\d)\.){3}(?:2[0-4]\d|25[0-5]|1\d{2}|[1-9]?\d)(?:\:(?:\d|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]))?$/g)) {
if(data.match(/:/g)) {
var split = data.split(':');
return split[0];
}
return data;
}
}
return null;
}
You can find all sorts of regex matching IP addresses on the web. The regex above also support including a port number as seen in the example above. If the data
value matches the regex, we return the IP part.
#javascript #ip #programming
1600671660
Javascript string split() is an inbuilt function that is used to split the string into an array of the substring and returns a new collection. If the empty string (“”) is used as the separator, the string is split between each character. The split() method does not mutate the original string.
The syntax is following for String Split Example.
string.split(separator, limit)
The first argument to the split function is the string which specifies the points where the split has to take place.
This argument can be treated as a simple string or as a regular expression. If the separator is unspecified, then the whole string becomes a single array element.
The same thing also happens when a separator is not present in the string. If the separator is an empty string (“”), then every character of the string is separated.
The second argument to a function limit defines an upper limit on the number of splits to be found in a given string. If a string remains unchecked after a threshold or limit is reached, then it is not reported in an array.
#javascript #javascript string.split #regular expression
1592956380
We recently did some improvements on the elmah.io UI making IPs clickable. We’ve had automatic detection of IPs for a while but it always bugged me that the regex that we had didn’t caught IP addresses with a port number (typically logged by ASP.NET MVC and similar):
127.0.0.1:80
The solution was an improved piece of JavaScript to detect this that I’m sharing in hope of helping others with a similar challenge:
function resolveIP(data) {
if (data) {
if(data.match(/^(?:(?:2[0-4]\d|25[0-5]|1\d{2}|[1-9]?\d)\.){3}(?:2[0-4]\d|25[0-5]|1\d{2}|[1-9]?\d)(?:\:(?:\d|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]))?$/g)) {
if(data.match(/:/g)) {
var split = data.split(':');
return split[0];
}
return data;
}
}
return null;
}
You can find all sorts of regex matching IP addresses on the web. The regex above also support including a port number as seen in the example above. If the data
value matches the regex, we return the IP part.
#javascript #ip #programming
1623050167
Everyone loves Mad Libs! And everyone loves Python. This article shows you how to have fun with both and learn some programming skills along the way.
Take 40% off Tiny Python Projects by entering fccclark into the discount code box at checkout at manning.com.
When I was a wee lad, we used to play at Mad Libs for hours and hours. This was before computers, mind you, before televisions or radio or even paper! No, scratch that, we had paper. Anyway, the point is we only had Mad Libs to play, and we loved it! And now you must play!
We’ll write a program called mad.py
which reads a file given as a positional argument and finds all the placeholders noted in angle brackets like <verb>
or <adjective>
. For each placeholder, we’ll prompt the user for the part of speech being requested like “Give me a verb” and “Give me an adjective.” (Notice that you’ll need to use the correct article.) Each value from the user replaces the placeholder in the text, and if the user says “drive” for “verb,” then <verb>
in the text replaces with drive
. When all the placeholders have been replaced with inputs from the user, print out the new text.
#python #regular-expressions #python-programming #python3 #mad libs: using regular expressions #using regular expressions
1601055000
Regular expressions is a powerful search and replace technique that you probably have used even without knowing. Be it your text editor’s “Find and Replace” feature, validation of your http request body using a third party npm module or your terminal’s ability to return list of files based on some pattern, all of them use Regular Expressions in one way or the other. It is not a concept that programmers must definitely learn but by knowing it you are able to reduce the complexity of your code in some cases.
_In this tutorial we will be learning the key concepts as well as some use cases of Regular Expressions in _javascript.
There are two ways of writing Regular expressions in Javascript. One is by creating a **literal **and the other is using **RegExp **constructor.
//Literal
const myRegex=/cat/ig
//RegExp
const myRegex=new RegExp('cat','ig')
While both types of expressions will return the same output when tested on a particular string, the benefit of using the RegExp
constructor is that it is evaluated at runtime hence allowing use of javascript variables for dynamic regular expressions. Moreover as seen in this benchmark test the RegExp
constructor performs better than the literal regular expression in pattern matching.
The syntax in either type of expression consists of two parts:
#regular-expressions #javascript #programming #js #regex #express
1622207074
Who invented JavaScript, how it works, as we have given information about Programming language in our previous article ( What is PHP ), but today we will talk about what is JavaScript, why JavaScript is used The Answers to all such questions and much other information about JavaScript, you are going to get here today. Hope this information will work for you.
JavaScript language was invented by Brendan Eich in 1995. JavaScript is inspired by Java Programming Language. The first name of JavaScript was Mocha which was named by Marc Andreessen, Marc Andreessen is the founder of Netscape and in the same year Mocha was renamed LiveScript, and later in December 1995, it was renamed JavaScript which is still in trend.
JavaScript is a client-side scripting language used with HTML (Hypertext Markup Language). JavaScript is an Interpreted / Oriented language called JS in programming language JavaScript code can be run on any normal web browser. To run the code of JavaScript, we have to enable JavaScript of Web Browser. But some web browsers already have JavaScript enabled.
Today almost all websites are using it as web technology, mind is that there is maximum scope in JavaScript in the coming time, so if you want to become a programmer, then you can be very beneficial to learn JavaScript.
In JavaScript, ‘document.write‘ is used to represent a string on a browser.
<script type="text/javascript">
document.write("Hello World!");
</script>
<script type="text/javascript">
//single line comment
/* document.write("Hello"); */
</script>
#javascript #javascript code #javascript hello world #what is javascript #who invented javascript