Ragen Dazs
Ragen Dazs

Reputation: 2170

Regex with fixed begin and end

I have the following stuff - and I'm trying get property calc(100% - 14px) with no luck:

var a = 'width:calc(100% - 14px); adsasd';
var b = 'width:calc(100% - 14px]; adsasd';

console.log(a.match('width:calc\(.*?\)$'));
console.log(b.match('width:calc\(.*?\)$'));

Example http://jsfiddle.net/zFacF/

Both console.log will output the same result

["width:calc(100% - 14px); adsasd", 
 "(100% - 14px); adsasd"] 

["width:calc(100% - 14px]; adsasd", 
 "(100% - 14px]; adsasd"] 

How can I get calc(100% - 14px) from given string?

@Solved with

/width:\s?(calc\(.*\))/

Because with inline style="width: calc(100% - 14px)" without ; and with spaces beteween width and calc not matches.

http://jsfiddle.net/zFacF/5/

Upvotes: 0

Views: 135

Answers (4)

user2264587
user2264587

Reputation: 474

/width:(calc\(.*\))/

if you want to account for square brackets:

/width:(calc[(\[].*[)\]])/

Upvotes: 1

Petr Behenský
Petr Behenský

Reputation: 620

If you want also to validate that string has same form use:

^width:(calc\(.*\));.*$

The first group (group[1]) will contain required result.

If you don't need validation use only:

calc\(.*\)

In this case result is whole match.

Upvotes: 1

Rick Kuipers
Rick Kuipers

Reputation: 6617

This seems to work alright: http://jsfiddle.net/zFacF/1/

The $ was making the .* match all the way to the end. Instead I added a ; to stop that from happening.

Upvotes: 1

Anirudha
Anirudha

Reputation: 32787

The $ at the end of your regex is streching your pattern match till end..$ depicts the end of string..

Use width:(calc\(.*?\));

Group 1 contains your required data

Upvotes: 1

Related Questions