Posted on • Originally published atdjangotricks.com on
Equivalents in Python and JavaScript. Part 2
Last time we started a new series of articles about analogies inPython andJavaScript. We had a look at lists, arrays, dictionaries, objects, and strings, conditional assignments, and parsing integers. This time we will go through more interesting and more complex things like serializing dictionaries and lists to JSON, operations with regular expressions, as well as raising and catching errors.
JSON
When working with APIs it is very usual to serialize objects to JSON format and be able to parse JSON strings.
InPython it is done with thejson
module like this:
importjsonjson_data=json.dumps(dictionary,indent=4)dictionary=json.loads(json_data)
Here we'll indent the nested elements in the JSON string by 4 spaces.
InJavaScript there is aJSON
object that has methods to create and parse JSON strings:
json_data=JSON.stringify(dictionary,null,4);dictionary=JSON.parse(json_data);
Splitting strings by regular expressions
Regular expressions are multi-tool that once you master, you can accomplish lots of things.
In the last article, we saw how one can join lists of strings into a single string. But how can you split a long string into lists of strings? What if the delimiter can be not a single character as the comma, but a range of possible variations? This can be done with regular expressions and thesplit()
method.
InPython, thesplit()
method belongs to the regular expression pattern object. This is how you could split a text string into sentences by punctuation marks:
importre# One or more characters of "!?." followed by whitespacedelimiter=re.compile(r'[!?\.]+\s*')text="Hello!!! What's new? Follow me."sentences=delimiter.split(text)# sentences == ['Hello', "What's new", 'Follow me', '']
InJavaScript thesplit()
method belongs to the string:
// One or more characters of "!?." followed by whitespacedelimiter=/[!?\.]+\s*/;text="Hello!!! What's new? Follow me.";sentences=text.split(delimiter)// sentences === ["Hello", "What's new", "Follow me", ""]
Matching regular expression patterns in strings
Regular expressions are often used to validate data from the forms.
For example, to validate if the entered email address is correct, you would need to match it against a regular expression pattern. InPython that would look like this:
importre# name, "@", and domainpattern=re.compile(r'([\w.+\-]+)@([\w\-]+\.[\w\-.]+)')match=pattern.match('hi@example.com')# match.group(0) == 'hi@example.com'# match.group(1) == 'hi'# match.group(2) == 'example.com'
If the text matches the pattern, it returns a match object with thegroup()
method to read the whole matched string, or separate captures of the pattern that were defined with the parenthesis. 0 means getting the whole string, 1 means getting the match in the first group, 2 means getting the match in the second group, and so on. If the text doesn't match the pattern, theNone
value will be returned.
InJavaScript thematch()
method belongs to the string and it returns either a match object, ornull
. Pretty similar:
// name, "@", and domainpattern=/([\w.+\-]+)@([\w\-]+\.[\w\-.]+)/;match='hi@example.com'.match(pattern);// match[0] === 'hi@example.com'// match[1] === 'hi'// match[2] === 'example.com'
The match object inJavaScript acts as an array. Its value at the zeroth position is the whole matched string. The other indexes correspond to the captures of the pattern defined with the parenthesis.
Moreover, sometimes you need to search if a specific value exists in a string and at which letter position it will be found. That can be done with thesearch()
method.
InPython this method belongs to the regular expression pattern and it returns the match object. The match object has thestart()
method telling at which letter position the match starts:
text='Say hi at hi@example.com'first_match=pattern.search(text)iffirst_match:start=first_match.start()# start == 10
InJavaScript thesearch()
method belongs to the string and it returns just an integer telling at which letter position the match starts. If nothing is found,-1
is returned:
text='Say hi at hi@example.com';first_match=text.search(pattern);if(first_match>-1){start=first_match;// start === 10}
Replacing patterns in strings using regular expressions
Replacing with regular expressions usually happen when cleaning up data, or adding additional features. For example, we could take some text and make all email addresses clickable.
Python developers would use thesub()
method of the regular expression pattern:
html=pattern.sub(r'<a href="mailto:\g<0>">\g<0></a>','Say hi at hi@example.com',)# html == 'Say hi at <a href="mailto:hi@example.com">hi@example.com</a>'
JavaScript developers would use thereplace()
method of the string:
html='Say hi at hi@example.com'.replace(pattern,'<a href="mailto:$&">$&</a>',);// html === 'Say hi at <a href="mailto:hi@example.com">hi@example.com</a>'
InPython the captures, also called as "backreferences", are accessible in the replacement string as\g<0>
,\g<1>
,\g<2>
, etc. InJavaScript the same is accessible as$&
,$1
,$2
, etc. Backreferences are usually used to wrap some strings or to switch places of different pieces of text.
It is also possible to replace a match with a function call. This can be used to do replacements within replacements or to count or collect some features of a text. For example, using replacements with function calls inJavaScript, I once wrote a fully functional HTML syntax highlighter.
Here let's change all email addresses in a text to UPPERCASE.
InPython, the replacement function receives the match object. We can use itsgroup()
method to do something with the matched text and return a text as a replacement:
text=pattern.sub(lambdamatch:match.group(0).upper(),'Say hi at hi@example.com',)# text == 'Say hi at HI@EXAMPLE.COM'
InJavaScript the replacement function receives the whole match string, the first capture, the second capture, etc. We can do what we need with those values and then return some string as a replacement:
text='Say hi at hi@example.com'.replace(pattern,function(match,p1,p2){returnmatch.toUpperCase();});// text === 'Say hi at HI@EXAMPLE.COM'
Error handling
Contrary toPython, client-sideJavaScript normally isn't used for saving or reading files or connecting to remote databases. Sotry..catch
blocks are quite rare inJavaScript compared totry..except
analogy inPython.
Anyway, error handling can be used with custom user errors implemented and raised inJavaScript libraries and caught in the main code.
The following example inPython shows how to define a custom exception classMyException
, how to raise it in a function, and how to catch it and handle in atry..except..finally
block:
classMyException(Exception):def__init__(self,message):self.message=messagedef__str__(self):returnself.messagedefproceed():raiseMyException('Error happened!')try:proceed()exceptMyExceptionaserr:print('Sorry! {}'.format(err))finally:print('Finishing')
The following example inJavaScript does exactly the same: here we define aMyException
class, throw it in a function, and catch it and handle in thetry..catch..finally
block.
functionMyException(message){this.message=message;this.toString=function(){returnthis.message;}}functionproceed(){thrownewMyException('Error happened!');}try{proceed();}catch(err){if(errinstanceofMyException){console.log('Sorry!'+err);}}finally{console.log('Finishing');}
TheMyException
class in both languages has a parametermessage
and a method to represent itself as a string using the value of themessage
.
Of course, exceptions should be raised/thrown just in the case of errors. And you define what is an error in your module design.
The Takeaways
- Serialization to JSON is quite straightforward in both,Python andJavaScript.
- Regular expressions can be used as multi-tools when working with textual data.
- You can do replacements with function calls in both languages.
- For more sophisticated software design you can use custom error classes.
As I mentioned last time, you can grab a side-by-side comparison ofPython andJavaScript that I compiled for you (and my future self). Side by side you will see features fromtraditional list, array, dictionary, object, and string handling tomodern string interpolation, lambdas, generators, sets, classes, and everything else. Use it for good.
Get the Ultimate Cheat Sheet of
Equivalents in Python and JavaScript
✨✨✨
In thenext part of the series, we will have a look at textual templates, list unpacking, lambda functions, iteration without indexes, generators, and sets. Stay tuned!
Cover photo byBenjamin Hung.
Top comments(1)

- LocationBerlin, Germany
- EducationVilnius University, Lithuania
- WorkFounder at "1st things 1st"
- Joined
For further actions, you may consider blocking this person and/orreporting abuse