RegEx to get text from inside the square brackets [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regular Expression to find a string included between two characters, while EXCLUDING the delimiters
I have a function where I have to get text which is enclosed in square brackets but not brackets for example
this is [test] line i [want] text [inside] square [brackets]
from the above line I want words:
test
want
inside
brackets
I am trying with to do this with /\[(.*?)\]/g but I am not getting satisfied result, I get the words inside brackets but also brackets which are not what I want
I did search for some similar type of question on SO but none of those solution work properly for me here is one what found (?<=\[)[^]]+(?=\]) this works in RegEx coach but not with JavaScript. Here is reference from where I got this
here is what I have done so far: demo
please help.

A single lookahead should do the trick here:
a = "this is [test] line i [want] text [inside] square [brackets]"
words = a.match(/[^[\]]+(?=])/g)
but in a general case, exec or replace-based loops lead to simpler code:
words = []
a.replace(/\[(.+?)\]/g, function($0, $1) { words.push($1) })

This fiddle uses RegExp.exec and outputs only what's inside the parenthesis.
var data = "this is [test] line i [want] text [inside] square [brackets]"
var re= /\[(.*?)\]/g;
for(m = re.exec(data); m; m = re.exec(data)){
alert(m[1])
}

Related

Extract text from enclosing parentheses using JavaScript [duplicate]

This question already has answers here:
Get text between two rounded brackets
(7 answers)
Closed 2 years ago.
I have a string like Manila (Philippines) and want to replace it with only the substring Philippines. I tried using the following regex pattern, which works in Notepad++:
[^\(]+ \(([^\)]+)\)
However, I get an undefined result in JavaScript:
var x = "Manila (Philippines)";
console.log(x.replace(/[^\(]+ \(([^\)]+)\)/,$1));
You just forgot the " around your replace pattern!
console.log(x.replace(/[^\(]+ \(([^\)]+)\)/,"$1")); will work correctly!
You can use .match():
var x = "Manila (Philippines)";
var result = x.match(/\((.+)\)/).pop();
// regex for string contained in parentheses
console.log(result);

How do I insert something at a specific character with Regex in Javascript [duplicate]

This question already has answers here:
Simple javascript find and replace
(6 answers)
Closed 5 years ago.
I have string "foo?bar" and I want to insert "baz" at the ?. This ? may not always be at the 3 index, so I always want to insert something string at this ? char to get "foo?bazbar"
The String.protype.replace method is perfect for this.
Example
let result = "foo?bar".replace(/\?/, '?baz');
alert(result);
I have used a RegEx in this example as requested, although you could do it without RegEx too.
Additional notes.
If you expect the string "foo?bar?boo" to result in "foo?bazbar?boo" the above code works as-is
If you expect the string "foo?bar?boo" to result in "foo?bazbar?bazboo" you can change the call to .replace(/\?/g, '?baz')
You don't need a regular expression, since you're not matching a pattern, just ordinary string replacement.
string = 'foo?bar';
newString = string.replace('?', '?baz');
console.log(newString);

javascript - Regex make string uppercase [duplicate]

This question already has answers here:
Replace a Regex capture group with uppercase in Javascript
(7 answers)
Closed 5 years ago.
I wonder if there is a way to make a string uppercase using regex only in JS.
The thing is that I am giving my users a string transformation system.
The user supply me with three parameters : original text, replace regex, subtitution regex.
for example:
original : 'stackoverflow'
replace : /([a-z])(.*)/g
subtitution : $1
Result : 's'
I want to give them the abilitty to set the entire string to uppercase. I've noticed in some other SO questions that there are systems that allows that. for example in sublime text you can do '/\U$1/' to set the entire string to uppercase.
Notice: I cannot use toUpperCase or toLowerCase in any way
Javascript has an inbuilt uppercasing method
var str = "Hello World!";
var res = str.toUpperCase();
The result of res will be:
HELLO WORLD!

match doesn't return capturing group [duplicate]

This question already has answers here:
Regex doesn't omit quotes when matching text in quotes
(3 answers)
Closed 7 years ago.
I'm trying to apply a regular expression to a string, in search of any placeholders, but I can't figure out why the result is only the full match, not the capturing group.
//----HTML------------------------//
<p>Let's try plaintext.<br/>
bommdidumpidudidoo</p>
<p id="output"></p>
//----JS------------------------//
var s = $('p').html();
var matches = s.match( /.*(plaintext)/g );
write(matches);
write(matches[0]);
write(matches[1]);
//------- whatever -------//
function write(s) {
$('#output').html( $('#output').html() +'<br/>'+s );
}
// output:
// Let's try plaintext
// Let's try plaintext
// undefined
» fiddle
(I've used my custom function write instead of console.log so that the result would show up in the fiddle)
Why is the third line undefined? I don't understand!
I'm pretty sure the expression is right. I'm 1oo% certain that this is the right capturing group syntax for JavaScript, and that .match() returns an array with the full match first, then all capturing groups. I've even tested it with regex101.com – over there, I do get the capturing group.
It's not the same as this other problem because there, the OR logic was the crux of the problem and here the pipe | doesn't even come up.
Oooh! It's because I'm doing a global search, with the g modifier. I do get the expected result if I remove that modifier. Tss.
I've used that modifier in the first place in order to grab multiple placeholders, but I guess I can still while-loop that stuff …

Replace with $$ in Javascript [duplicate]

This question already has answers here:
Javascript replace issue with $ [duplicate]
(7 answers)
Closed 8 years ago.
I would like to replace all occurences of a particular string with $$ in javascript.
However when I attempt the replace it only replaces a single character
http://jsfiddle.net/PrZ9y/
text = "sfsd";
text =text.replace(/sf/gi,"\$\$");
document.getElementById("x").innerHTML=text;
This for example outputs $sd. The correct output should be $$sd
I also tried
text =text.replace(/sf/gi,"$$");
Since $ is reserved for regular expression, doubling-up on $ will escaped it's intended purpose.
text = "sfsd";
text = text.replace(/sf/gi,"$$$$");
document.getElementById("x").innerHTML = text;
Dollar sign has a special meaning, it means the number of captured group when used in conjuction with a number, like $1. $$ is just the literal for $, the first $ will play the role of escape character. Use $$$$ for $$.
More info:
http://es5.github.io/#x15.5.4.11
Try putting your html as:
<div id="x">sf sd</div>
<div id="wmd-input"></div>
Then, change your code to:
text = document.getElementById("x").innerHTML;
text = text.replace(/sf/gi,"$$$$");
document.getElementById("wmd-input").innerHTML = text;
jsfiddle.
As #Mr Polywhirl noted, you need to use another $ to escape the $ and get one literal $ sign.
$$ is reserved just like $& is reserved, so use three of them:
text=text.replace(/sf/gi,"$$$");

Categories

Resources