Equivalents in Python and JavaScript. Part 2
Tags: intermediate, advanced, python, javascript
Published: 2018-07-03 11:10:28

---
title: Equivalents in Python and JavaScript. Part 2
published: true
tags: Intermediate,Advanced,Python,JavaScript
canonical_url: http://djangotricks.blogspot.com/2018/07/equivalents-in-python-and-javascript-part-2.html
series: Equivalents in Python and JavaScript
---

 ![](https://4.bp.blogspot.com/-ZEZgva-20Og/WzrCcYKB-NI/AAAAAAAAB5w/8Pv9p7uP_kYXxEL-Y_wkFxvTtU_ZXJK6gCLcBGAs/s1600/equivalents-in-python-and-javascript-part-2.png)

[Last time](https://dev.to/djangotricks/equivalents-in-python-and-javascript-part-1-3317) we started a new series of articles about analogies in __Python__ and __JavaScript__. 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.

In __Python__ it is done with the `json` module like this:

```python
import json
json_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.

In __JavaScript__ there is a `JSON` object that has methods to create and parse JSON strings:

```javascript
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 the `split()` method.

In __Python__, the `split()` method belongs to the regular expression pattern object. This is how you could split a text string into sentences by punctuation marks:

```python
import re

# One or more characters of "!?." followed by whitespace
delimiter = re.compile(r'[!?\.]+\s*')

text = "Hello!!! What's new? Follow me."
sentences = delimiter.split(text)
# sentences == ['Hello', "What's new", 'Follow me', '']
```

In __JavaScript__ the `split()` method belongs to the string:

```javascript
// One or more characters of "!?." followed by whitespace
delimiter = /[!?\.]+\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. In __Python__ that would look like this:

```python
import re

# name, "@", and domain
pattern = 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 the `group()` 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, the `None` value will be returned.

In __JavaScript__ the `match()` method belongs to the string and it returns either a match object, or `null`. Pretty similar:

```javascript
// name, "@", and domain
pattern = /([\w.+\-]+)@([\w\-]+\.[\w\-.]+)/;

match = 'hi@example.com'.match(pattern);
// match[0] === 'hi@example.com'
// match[1] === 'hi'
// match[2] === 'example.com'
```

The match object in __JavaScript__ 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 the `search()` method.

In __Python__ this method belongs to the regular expression pattern and it returns the match object. The match object has the `start()` method telling at which letter position the match starts:

```python
text = 'Say hi at hi@example.com'
first_match = pattern.search(text)
if first_match:
    start = first_match.start()  # start == 10
```

In __JavaScript__ the `search()` 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:

```javascript
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 the `sub()` method of the regular expression pattern:

```python
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 the `replace()` method of the string:

```javascript
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>'
```

In __Python__ the captures, also called as "backreferences", are accessible in the replacement string as `\g<0>`, `\g<1>`, `\g<2>`, etc. In __JavaScript__ 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 in __JavaScript__, I once wrote a fully functional HTML syntax highlighter.

Here let's change all email addresses in a text to UPPERCASE.

In __Python__, the replacement function receives the match object. We can use its `group()` method to do something with the matched text and return a text as a replacement:

```python
text = pattern.sub(
    lambda match: match.group(0).upper(), 
    'Say hi at hi@example.com',
)
# text == 'Say hi at HI@EXAMPLE.COM'
```

In __JavaScript__ 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:

```javascript
text = 'Say hi at hi@example.com'.replace(
    pattern,
    function(match, p1, p2) {
        return match.toUpperCase();
    }
);
// text === 'Say hi at HI@EXAMPLE.COM'
```

## Error handling

Contrary to __Python__, client-side __JavaScript__ normally isn't used for saving or reading files or connecting to remote databases. So `try..catch` blocks are quite rare in __JavaScript__ compared to `try..except` analogy in __Python__.

Anyway, error handling can be used with custom user errors implemented and raised in __JavaScript__ libraries and caught in the main code.

The following example in __Python__ shows how to define a custom exception class `MyException`, how to raise it in a function, and how to catch it and handle in a `try..except..finally` block:

```python
class MyException(Exception):
    def __init__(self, message):
        self.message = message
        
    def __str__(self):
        return self.message
        
def proceed():
    raise MyException('Error happened!')

try:
    proceed()
except MyException as err:
    print('Sorry! {}'.format(err))
finally:
    print('Finishing')    
```

The following example in __JavaScript__ does exactly the same: here we define a `MyException` class, throw it in a function, and catch it and handle in the `try..catch..finally` block.

```javascript
function MyException(message) {
   this.message = message;
   this.toString = function() {
       return this.message;
   }
}

function proceed() {
    throw new MyException('Error happened!');
}

try {
    proceed();
} catch (err) {
    if (err instanceof MyException) {
        console.log('Sorry! ' + err);
    }
} finally {
    console.log('Finishing');
}

```

The `MyException` class in both languages has a parameter `message` and a method to represent itself as a string using the value of the `message`.

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__ and __JavaScript__.
- 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 of __Python__ and __JavaScript__ that I compiled for you (and my future self). Side by side you will see features from __traditional__ list, array, dictionary, object, and string handling to __modern__ string interpolation, lambdas, generators, sets, classes, and everything else. Use it for good.

<center>✨✨✨<br /><a href="https://www.djangotricks.com/goodies/YbnpiLKBmAZi/" target="_blank" >Get the Ultimate Cheat Sheet of<br />Equivalents in Python and JavaScript<br />✨✨✨</a></center>

In the [next part](https://dev.to/djangotricks/equivalents-in-python-and-javascript-part-3-4icc) 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 by [Benjamin Hung](https://unsplash.com/photos/fs9hGJUevXY).

 ![](http://feeds.feedburner.com/~r/djangotricks/~4/QSH4HQuebtY)