例如:
“10.0.cm”转到“10.0”
和
“10.0.m”转到“10.0”
和
“3”保持为“3”
等...
我试过了:
values[3].replace(/[^0-9.,]+/, '')
但这仍然留下了“。”在数字之后,例如:10.0。
感谢您的帮助......
答案 0 :(得分:2)
var values = ["10.0.cm", "10.0.m", "3"];
var patten = /\d+(\.\d+)?/g;
for (var i = 0; i < values.length; i++) {
console.info(values[i].match(patten));
}
答案 1 :(得分:2)
以下正则表达式应该解决它
- (NSURLSessionTask *) startUploadTaskForURL: (NSURL *) url withData: (NSData *) data {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request addValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
NSURLSessionUploadTask uploadTask = [self.session uploadTaskWithRequest:request fromData:data];
[self scheduleTask:task];
return task;
}
- (void) scheduleTask: (NSURLSessionTask *) task {
[self.runningTasks setObject:[NSMutableData data] forKey:task];
[task resume];
}
- (NSMutableData *) dataForTask: (NSURLSessionTask *) task {
return [self.runningTasks objectForKey:task];
}
- (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"HTTPClient: Data task received data: %@", dataString);
NSMutableData *runningData = [self dataForTask:dataTask];
if (!runningData) {
NSLog(@"No data found for task");
}
[runningData appendData: data];
}
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
NSLog(@"HTTPClient: Task completed with error: %@", error);
// my client also works with a delegate, so as to make it reusable
[self.delegate sessionTask:task completedWithError:error data:[self dataForTask:task]];
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
NSLog(@"HTTPClient: did send body data: %lld of %lld bytes ", totalBytesSent, totalBytesExpectedToSend);
}
答案 2 :(得分:1)
您所描述的问题不是简单的“在字符串中查找数字”
一步完成此操作并不容易,因为 JavaScript 中的 RegExp 不支持回溯。然而
.
var str = 'foo_1.0.bar.1.2.xyz';
str = str.replace(/[^\d\.]/g, '').split('.');
str = str[0] ? str[0] + '.' + str.slice(1).join('') : '';
str; // "1.012"