Skip to content

Commit 6fb649e

Browse files
merge: Add ValidateUrl in String (TheAlgorithms#856)
* Add ValidateUrl in String * change regex * Bug fix
1 parent 961f21f commit 6fb649e

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

String/ValidateUrl.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @function ValidateURL
3+
* @description validate the URL.
4+
* @param {String} url - The input URL string
5+
* @return {Boolean}
6+
*/
7+
const validateURL = (url) => {
8+
const URL_PATTERN = /^(https?:\/\/(?:www\.|(?!www))[^\s.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})$/gi
9+
10+
return URL_PATTERN.test(url)
11+
}
12+
13+
export { validateURL }

String/test/ValidateUrl.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { validateURL } from '../ValidateUrl'
2+
3+
describe('ValidateUrl', () => {
4+
it('expects to return false', () => {
5+
expect(validateURL('google')).toEqual(false)
6+
expect(validateURL('link: https://www.google.com')).toEqual(false)
7+
})
8+
9+
it('expects to return true', () => {
10+
expect(validateURL('http://www.google.com')).toEqual(true)
11+
expect(validateURL('https://www.google.com')).toEqual(true)
12+
expect(validateURL('www.google.com')).toEqual(true)
13+
})
14+
})

0 commit comments

Comments
 (0)