我有一个网址:http://localhost:8080/school/teacher/reports/chapter-quiz/student
获取最后一段,我只需要这样做
var lastSegment = location.pathname.split('/').pop();
但是如何抓住最后一个旁边的?的章-测验
答案 0 :(得分:4)
我说这样的话?
var segments = location.pathname.split('/');
secondLastSegment = segments[segments.length - 2];
答案 1 :(得分:2)
var segment_1 = pathArray[1];
var segment_2 = pathArray[2];
var segment_3 = pathArray[3];
var segment_4 = pathArray[4];
console.log(segment_4) --> chapter-quiz
{{1}}
答案 2 :(得分:0)
你可以用这个
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 640, HEIGHT = WIDTH/12*9;
public static boolean running = false;
Thread thread;
private Handler handler;
public Game(){
new Window(WIDTH,HEIGHT, "This Is My Game", this);
handler = new Handler();
handler.addObject(new Player(100,100, ID.Player));
}
public synchronized void start(){
thread = new Thread(this);
thread.start();
running = true;
}
public synchronized void stop(){
try{
thread.join();
running = false;
}catch(Exception e){
e.printStackTrace();
}
}
public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if(delta >= 1){
tick();
delta--;
}
if(running){
render();
frames++;
}
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
System.out.println("Fps: " + frames);
frames = 0;
}
}
stop();
}
public void tick(){
handler.tick();
}
public void render(){
BufferStrategy bs = this.getBufferStrategy();
if (bs == null){
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
handler.render(g);
g.setColor(Color.white);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.dispose();
bs.show();
}
public static void main(String args[]){
new Game();
}
}
public class Player extends GameObject {
public Player(int x, int y,ID id) {
super(x, y, id);
}
public void tick() {
}
public void render(Graphics g) {
g.setColor(Color.black);
g.fillRect(x, y, 32, 32);
}
}
public abstract class GameObject {
protected int x, y;
protected ID id;
protected int velX, velY;
public GameObject(int x,int y,ID id){
this.x = x;
this.y = y;
this.id = id;
}
}
public class Handler {
LinkedList<GameObject> object = new LinkedList<GameObject>();
public void tick(){
for(int i = 0; i < object.size();i++){
GameObject tempObject = object.get(i);
tempObject.tick();
}
}
public void render(Graphics g){
for(int i = 0; i < object.size();i++){
GameObject tempObject = object.get(i);
tempObject.render(g);
}
}
public void addObject(GameObject object){
this.object.add(object);
}
public void removeObject(GameObject object){
this.object.remove(object);
}
}
答案 3 :(得分:0)
此函数将返回路径的特定部分,从末尾开始计算
pathFromEnd(url, 0)
返回最后一部分,pathFromEnd(url, 1)
返回倒数第二部分,依此类推......它理解url语法:有/无协议,尾随斜杠,参数......
function pathFromEnd(url, pos) {
return url.match(new RegExp("^(?:.*?:\\/\\/)?[^\\/]*(?:\\/([^\\/?]*))*?(?:\\/[^\\/?]*){" + pos + "}\\/?(?:\\?.*)?$"))[1];
}
alert(pathFromEnd("http://localhost:8080/school/teacher/reports/chapter-quiz/student", 1));
alert(pathFromEnd("localhost:8080/school/teacher/reports/chapter-quiz/student/", 1));
alert(pathFromEnd("www.example.com/school/teacher/reports/chapter-quiz/student?a=b", 1));
&#13;