I want to split on dot (.
) but I don’t want to splits the links.
Let’s say the string is –
JavaScript
x
2
1
<p>This is a paragraph. I want to split it. <a href="somesite.com">Link</a>
2
Expected Output –
JavaScript
1
2
1
'<p>This is a paragraph' ,'I want to split it' ,'<a href="somesite.com">Link</a>'
2
Current Output –
JavaScript
1
2
1
'<p>This is a paragraph' ,'I want to split it' ,'<a href="somesite', 'com">Link</a>'
2
Note that I don’t want the link to split. Also, I know you can split it using .split(".")
but how can I not split that link?
Advertisement
Answer
Solution 1: Strings objects have a method called ‘split’:
JavaScript
1
8
1
s = 'google.com'
2
3
splitted = s.split('.')
4
5
print(splitted)
6
7
>>> ['google', 'com']
8
That takes a string and split by a substring such as ‘.’.
Solution 2: find the position of ‘.’ in the string, then split it manually:
JavaScript
1
14
14
1
s = 'google.com'
2
3
idx = s.indexOf('.')
4
5
first = s[:idx]
6
7
sec = s[idx:]
8
9
print(first)
10
>>> google
11
12
print(sec)
13
>>> .com
14