top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to write regex expression in javascript?

+1 vote
410 views

Please give some example for each.

posted Jul 10, 2014 by Amarvansh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used with the exec and test methods of RegExp, and with the match, replace, search, and split methods of String.

You construct a regular expression in one of two ways:

1)Using a regular expression literal, as follows:

var re = /ab+c/;

2)Calling the constructor function of the RegExp object, as follows:

 var re = new RegExp("ab+c");

Examples:

function escapeRegExp(string){
  return string.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1");
}

Methods that use regular expressions

exec
A RegExp method that executes a search for a match in a string. It returns an array of information.
test
A RegExp method that tests for a match in a string. It returns true or false.
match
A String method that executes a search for a match in a string. It returns an array of information or null on a mismatch.
search
A String method that tests for a match in a string. It returns the index of the match, or -1 if the search fails.
replace
A String method that executes a search for a match in a string, and replaces the matched substring with a replacement substring.
split
A String method that uses a regular expression or a fixed string to break a string into an array of substrings.

answer Aug 11, 2014 by Sidharth Malhotra
Similar Questions
0 votes

I tried the following code.Both onpaste() and Oncopy() will trigger when user trying to copy or paste.

But my problem is while copying something window.getSelection() not working.

Window.getSelection() is used to get the selected data.

I mean before copying something user need to select some values.So,using this window.getselection() we will get the value what they are selected.

Here, my problem is window.getselection() will work in oncopy but not working in onpaste.

Check the below examples and help me to solve the problem.

Also ,is there any alternative method also please let me know.I also tried window.clipboardData.But not getting the solution for this.

<!doctype html>
<html>
<head>
<style>
    textarea {
        width:500px;
        height:200px;
        border:2px solid #999;
    }
</style>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    <script type="text/javascript">

        function OnPaste () {                                                                       
            var selection = window.getSelection();          
            alert(selection.toString());            
        }                                       
        function OnCopy() {         
            var selection = window.getSelection();
            alert(selection.toString());
         }

// Call the OnCopy function while user trying to copy 
document.oncopy = OnCopy;

// Call the OnPaste function while user trying to Paste
document.onpaste = OnPaste;
 </script>

</head>
<body>
    Select some text, copy it to the clipboard and try to paste it to the following fields:
    <br /><br />
    <textarea size="40" value="Copy and Paste operation is enabled">
0 votes

I need to execute some function while pasting in web page. Can any tell me to solve this issue?This can be possible in Java Script or Jquery?

+5 votes

I need disable all refresh events

Example.

  1. F5,
  2. Mouse right click option (refresh/reload),
  3. CTRL+R,
  4. Menu bar -> view -> refresh and
  5. URL refresh/reload button

How to disable these all events by using javascript...

Advance thanks....

0 votes

Also,tell me what are the types of framework are there ?

...