admin管理员组

文章数量:1402943

I have some word have to split, but how can i split two things using one time only ? Example, "avg/hulk.swf" and "dance.swf" both I only need their name hulk and dance , How to write split jquery in one time to split avg/ and .swf that I want ? Because all get from DB , so some contain avg/ and .swf some only .swf .

var test = "avg/hulk.swf";
var keyword = test.split("avg/")[1];

alert(keyword);
<script src=".1.1/jquery.min.js"></script>
<div class="show"></div>

I have some word have to split, but how can i split two things using one time only ? Example, "avg/hulk.swf" and "dance.swf" both I only need their name hulk and dance , How to write split jquery in one time to split avg/ and .swf that I want ? Because all get from DB , so some contain avg/ and .swf some only .swf .

var test = "avg/hulk.swf";
var keyword = test.split("avg/")[1];

alert(keyword);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="show"></div>

Share Improve this question asked Apr 10, 2015 at 8:01 FeelRightzFeelRightz 2,9992 gold badges43 silver badges80 bronze badges 2
  • you go to regular expression – Sudharsan S Commented Apr 10, 2015 at 8:05
  • so your problem is that keyword sometimes contains ".swf"? Why not use an other split to get rid of that the same way you got rid of "avg/"? – doldt Commented Apr 10, 2015 at 8:06
Add a ment  | 

4 Answers 4

Reset to default 2

Try to remove them from string.

var test = "avg/hulk.swf";
var keyword = test.replace(/avg\/|\.swf/g,'');

alert(keyword);

try this.

you can use split() then pop() andslice() after

    function getName(path){
      return (path.split('/').pop()).slice(0,-4); 
    }

    alert( getName("dance.swf") + "\n" + getName("avg/hulk.swf")); 

SEE DEMO

its only works for your given context.

var test = "avg/hulk.swf";
var keyword = test.split("avg/")[1].split(".")[0];

alert(keyword);

Fiddle

1    var str = "JQUERY By Example";

2    var n = str.charAt(2)



//Output will be "U"

so yeah here is an example for 1 letter, and here is if you want to get a specific word:

var str="Hello world!";
var n=str.substr(2,3)
//Output will be "llo"

Hope this helps!

本文标签: javascriptSplit word jqueryStack Overflow