0

HI i need to split some part of variable value

in my html file i got a dynamic value of variable some thing like this

product/roe_anythin_anything-1.jpg 

product/soe_anything_anything-2.jpg

i need to remove the before

/slashpart

and after

_ part

which should return the roe or soe part

i have use a function

  <script>
function splitSize(){

$('#splitSize').each(function(index) {
    var mystr = $(this).html();
    var mystr1 = /product\/(.*)-.*/.exec(mystr);
    $(this).html(mystr1[1]);
    //$(this).html(mystr1[0]);  
});
}
splitSize();


</script>

with which i got roe_anythin_anything successfully i just need to remove now after `

_ part

`

please suggest how can i do this

user2106353
  • 1,191
  • 3
  • 10
  • 18

2 Answers2

1

This is as you asked using split . You can use RegEx to make it simpler

 var myStr = 'product/roe-1.jpg' ;
 myStr = myStr.split('/')[1];
 myStr = myStr.split('-')[0];

Working JS Fiddle

Prasath K
  • 4,950
  • 7
  • 23
  • 35
  • 1
    Or with an regular expression in one line: `myStr.split(/\/|-/)[1];` - [fiddle](http://jsfiddle.net/xCUB8/) – Andreas Apr 26 '13 at 10:35
0

Use regex group capture

var myStr = 'product/roe-1.jpg';
var result = /product\/(.*)-.*/.exec(myStr)[1];

Break down:

/product\/

matches the initial product string and the / character (escaped so its not interpreted as the end of the regex)

The

(.*)

Matches your roe characters and keeps them in a 'capture group' - everything up to but not including the hyphen

Then the hyphen is matched, then anything else.

This returns a 2 element array. Item 0 is the whole string, item 1 is the contents of the capture group.

See How do you access the matched groups in a JavaScript regular expression? for more details

Community
  • 1
  • 1
Mike Hogan
  • 9,933
  • 9
  • 41
  • 71
  • this is my string product/tammy_av_pewter-1_1.jpg byusing your i got tammy_av_pewter how to remove _av_pewter also but thanks for now help – user2106353 Apr 26 '13 at 11:06