Javascript regular expressions troubling issue

function isOembed(url) {
    var pattern = /http:\/\/(.*youtube\.com\/watch.*|.*\.youtube\.com\/v\/.*|youtu\.be\/.*|.*\.youtube\.com\/user\/.*|.*\.youtube\.com\/.*#.*\/.*|m\.youtube\.com\/watch.*|m\.youtube\.com\/index.*|.*\.youtube\.com\/profile.*/;
    return pattern.test(url);
}

alert(isOembed('http://www.youtube.com/watch?v=9W8Ie4MyRX0&feature=related'));

I am encountering an issue where the following regular expression does not produce any output. I tested it on jsFiddle.

The error message states:

Error:
Problem at line 2 character 223: Unescaped '/'.
var pattern =/http:\/\/(.*youtube\.com\/watch.*|.*\.youtube\.com\/v\/.*|y...

Implied global: alert 6

Answer №1

Joannnnnie was absolutely correct; just make sure to include a closing parentheses at the end of the regex pattern. Here is an example: http://jsbin.com/ufufed/2/

var regexPattern = /http:\/\/(.*youtube\.com\/watch.*|.*\.youtube\.com\/v\/.*|youtu\.be\/.*|.*\.youtube\.com\/user\/.*|.*\.youtube\.com\/.*#.*\/.*|m\.youtube\.com\/watch.*|m\.youtube\.com\/index.*|.*\.youtube\.com\/profile.*)/;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Change a particular value in redux-form using a custom function callback within react-admin

I've made changes to the onDrop function within the ImageInput component to return a custom URL from my own function. My goal is to assign this returned value to the image prop of the ImageInput. ... const uploadImg = ( acceptedFiles ) => { ...

Encountering an 'Uncaught SyntaxError: Unexpected token' error while attempting to parse JSON data

I keep encountering an 'Uncaught SyntaxError' when attempting to parse a JSON string, and the reason behind it remains elusive. The issue seems to be related to the fact that message is recognized as a string, which is a known problem, even thou ...

What is the best way to display service data as soon as it is received using $http, without relying on angular.copy?

My challenge is with a service that is supposed to store specific data loaded from a JSON file and display it on the webpage immediately upon receiving. I attempted to set a $scope variable equal to the data in the service, but the data did not show up rig ...

Transferring data to parent component in React Native

Task Management Challenge In my current project, I am faced with a task management challenge. The parent component holds an array of tasks using the useState hook. Each task is then displayed in a child component along with a delete button for each indivi ...

The functionality for navigating the Angular uib-dropdown using the keyboard is currently experiencing issues

I am currently utilizing Angular Bootstrap 2.2.0 in conjunction with Angular 1.5. Despite enabling the keyboard-nav option, I am experiencing issues with keyboard navigation on UIB dropdowns. Below is the snippet of my code: <div class="btn-group" ...

Struggling with the placement of mesh cubes in three.js on varying screen sizes

One thing I've noticed is that objects get improperly positioned on different screen sizes. A good example of this can be seen by visiting the following page: When viewed on a larger laptop screen, the cubes are arranged correctly. However, on a sma ...

Arranging functions in descending and ascending order

I am working on a Table component that has an array of columns and data. I need to sort the columns and update the table state accordingly. My method takes two arguments key (column key) and sortable (true or false indicating if the column is sortable or ...

Having trouble fetching data using $http and promises in AngularJS

I'm having trouble connecting to my RESTful API within my AngularJS application. Despite my efforts, I'm not seeing any data being displayed on the screen. It seems like I might be misunderstanding the usage of $http with promises. Any suggestio ...

Utilizing this method for declaring functions in Angular

I'm currently learning about Angular and attempting to implement a controller. However, I've run into an issue where the ng-click is not working (the function is not being called). According to the Chrome extension Batarang, the function is defin ...

The Google Maps Javascript API will only load data if the console is open

I'm facing an issue with displaying a map on my webpage where I want to set a marker. What's the problem Despite no errors in the console or from Google, the map data is not loading properly. All I see is the Google logo and a grey background, ...

Unable to update the React state on a component that is unmounted when using HOOK

I'm currently working on a user authentication code in React, specifically for an Expo project. function App(){ const [user, setUser] = useState(null); if (user){ return <Dashboard user={user} /> } return <Authentication ...

Github npm package with incomplete files

I want to use logary-js in my project, which is available on GitHub (DISCLAIMER: my own project). Here is a snippet from my packages.json file: ... "dependencies": { "logary": "logary/logary-js#master", ... } ... However ...

I'm having trouble with populating data in Mongoose. Can anyone provide guidance on

I am currently working on a simple CRUD example for a larger open-source project. I'm trying to populate user data, but despite consulting documentation and forums, I haven't been able to resolve the issue that's causing trouble. The error ...

Tips for choosing DOM elements within the map function in a React component

I am working on a simple component where I need to select all inputs and listen for changes, but unfortunately, I am only getting a null node list. I really need to access the selected inputs. How can I achieve this within the map function? const Comp ...

Enhancing JSX Component with Transition Effect in React

Is there a way to incorporate a transition effect when the button is clicked within this React component? Since the code modifies the JSX content instead of the className, traditional CSS transitions may not work. How can I implement a smooth transition ...

Which is more efficient for rendering performance: using images, CSS gradients, or box shadows with borders?

I'm curious about improving website scroll and animation performance. Which option would be better for your mobile webapp or website: Using a repeating thin image or CSS3 gradient? or Utilizing a repeating image instead of box shadow with a borde ...

Challenges in Displaying Components in React with Typescript

I'm currently facing an issue where the content I am trying to render on my screen is not appearing. Although the function correctly enters the if conditional statement, as confirmed by console logging. This is the section where I have implemented th ...

Ways to extract transparency data of each pixel from a PNG uploaded by the user via the front-end

Currently, I am in need of extracting the transparency value for each individual pixel from a PNG image that is submitted by the user, directly within the front-end environment. At the moment, I have implemented a method where I prevent the form from bein ...

Retrieving specific items based on property and narrowing down a collection of objects using Mongoose

I'm currently developing a search feature that allows users to input a specific query and retrieve all messages containing that query. The search process involves sending a query to the database based on the current message context when the user init ...

How can I correctly rotate an object around a specific point using three.js?

Most tutorials/questions about three.js suggest using a parent object to rotate an object around a specific point. This involves creating a parent object at the desired position, attaching the object to it, and then rotating the parent to achieve the desir ...