Reputation: 1490
I am new to reg exp I have searched lot about pattern to find the circle brackets in the string value finally i found. But this was not working when i am getting the value from DIV
Here is the javascript code
function myText(){
var inputString = document.getElementById("text").innerHTML;
var result = inputString.match(/\(+/g);
alert(result);
}
myText();
HTML code
<div id="text">
welcome to (MotherLand)
</div>
I tried with removing .value from the inputString
I am getting the result as null in alert. I tried with removing .value from the inputString
Iam getting the error in console window Cannot read property 'match' of undefined
I am getting the result as null in alert
I want only the circle brackets not inside the value.
The output should be (
Kindly explain me what I am doing wrong.
fiddle link
Thanks Mahadevan
Upvotes: 1
Views: 879
Reputation: 59224
Here are the things I see wrong:
You need to get the value like this.
var inputString = document.getElementById("text").innerHTML;
The expression you want to find the text between "circle brackets" is:
var result = inputString.match(/\(([^\)]+)\)/);
\( // Match open parenthesis
( // Start a capture group
[^\)]+ // Match any character except a close parenthesis, repeating.
) // End capture group
\) // Match close parenthesis
result
will be an array. The first position will be the entire match and subsequent positions in the array will be any capture groups from your expression. So to get the text from between the parenthesis you would access result[1]
.
I had trouble duplicating it in your jsFiddle because of the arabic characters. You may have to find the actual unicode values of those characters and use them in your regular expression. Insert Unicode character into JavaScript
Here's a demo without the arabic characters: http://jsfiddle.net/tvxmrLgv/7/
Upvotes: 1
Reputation: 384
If you are trying to get the string with in the ()
, then you could use the following regex Pattern
var inputString = document.getElementById("text").innerHTML
var result = inputString.match(/\(([^)]+)\)/);
//result[0] contains (Motherland)
//result[1] contains Motherland
Upvotes: 2